Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Custom Form Rendering

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 6533 Views
  • 8mike8
    Avatar
    Community Member
    2 Posts

    Custom Form Rendering Link to this post

    Hi!

    What i like to know is: How is it possible to create custom forms?
    I like to display a custom form, but it seems that the form attributes wouldn't be parsed...

    So, here is my setup:
    1. I have my custom Page (KMForm_Page) / PageController (KMFormPage_Controller)
    The Controller looks like this:
    class KMFormPage_Controller extends Page_Controller
    {
    function KMForm()
    {
    return new KMForm($this,'KMForm');

    }

    }

    2. I do of course have a custom Form (KMForm) as used above:
    class KMForm extends Form
    {
    function __construct($controller, $name)
    {
    $fields = new FieldSet(
    new EmailField('email', 'E-Mail-Adresse'),
    new TextField('telefon', 'Telefonnummer')
    );

    $actions = new FieldSet(
    new FormAction('submit', 'Absenden')
    );

    $requiredFields = new RequiredFields('email');

    parent::__construct($controller, $name, $fields, $actions, $requiredFields);
    }

    function forTemplate()
    {
    // Erwartet Template mit Namen 'KMForm'
    return $this->renderWith(array(
    $this->class,
    'Form'
    ));
    }

    function submit($data, $form)
    {
    print_r($data);
    }
    }

    3. I have a appropriate Template KMForm.ss:
    <form $FormAttributes>
    <% if Message %>
    <p id="{$FormName}_error" class="message $MessageType">$Message</p>
    <% else %>
    <p id="{$FormName}_error" class="message $MessageType" style="display: none;"></p>
    <% end_if %>

    <fieldset>
    <div id="email" class="field email">
    <label for="$Form_email">E-Mail-Adresse</label>
    $dataFieldByName(email)
    </div>
    <div id="telefonnummer" class="field telefon">
    <label for="$FormName_telefon">Telefonnummer</label>
    $dataFieldByName(telefon)
    </div>
    $dataFieldByName(SecurityID)
    </fieldset>

    <% if Actions %>
    <div class="Actions">
    <% control Actions %>
    $Field
    <% end_control %>
    </div>
    <% end_if %>

    </form>

    Site displays, but:
    - The Rendering of the Variables $FormAttributes, $FormName, $Form_email etc. doesn't happen at all
    - site displays only the non-dynamic items of KMForm.ss

    So, as usual the failure sits in front of the PC...
    Any hints, what i'm doing wrong?

    Thanks for your time!

  • jahbini
    Avatar
    Community Member
    13 Posts

    Re: Custom Form Rendering Link to this post

    I've gotten past the point you have described in this posting (but am stumped by not activating the submit routine, got any hints on that?).

    Question: Do you have a $KMForm in the page layout template for KMFormPage_Controller?

    I would also advise putting a <% debug %> in both 'ss' files to see what methods are available. Most likely the 'ss' file is being activated on the wrong class object.

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Custom Form Rendering Link to this post

    Not 100% sure, however you may try following two points:
    1. make sure KMForm.ss is in 'templates/Includes' directory.
    2. it seems that you form function and form class has the same name, what I did recently is given the form subclass a different name. ie MyForm extends Form, then in your page controller, do

    function MyFirstForm(){
          return new MyForm($this, 'MyFirstForm');
       }

    In your case, maybe it is better to change sub class name, because you need KMForm.ss match the function name.

    Hope it will help

  • leafchild
    Avatar
    Community Member
    41 Posts

    Re: Custom Form Rendering Link to this post

    I have similar issue, and I need help!!!

    My code is pretty similar to what she posted
    (http://silverstripe.org/form-questions/show/16121#post301015)
    Also I smpled this page: http://pastie.org/522439

    #1. MyForm.ss is in templates/Includes directory
    #2. In my page controller, I just put this

    function MyFirstForm(){
    return new MyForm($this, 'MyFirstForm');
    }

    MyForm.ss is display fine but "$dataFieldByName()" is NOT working.

    I have this in my MyForm.php

    $fields = new FieldSet(
    new TextField('FirstName', 'First name'),
    new EmailField('Email', 'Email address')
    );


    so I have

    $dataFieldByName(FirstName)
    $dataFieldByName(Email)


    in my MyForm.ss but they are not displaying anything.
    It supposed to show

    <input type='text' ..... >

    right?

    What is wrong?

  • TDNP
    Avatar
    Community Member
    19 Posts

    Re: Custom Form Rendering Link to this post

    I was having similar issues. Check this forum post. It was my answer:
    http://doc.silverstripe.org/sapphire/en/topics/forms#using_a_custom_template

    6533 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.