Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » form template

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

Page: 1
Go to End
Author Topic: 1512 Views
  • leafchild
    Avatar
    Community Member
    41 Posts

    form template Link to this post

    Someone help! I have a problem creating form template.

    I checked:
    http://doc.silverstripe.org/sapphire/en/topics/forms
    http://silverstripe.org/form-questions/show/8333

    but I still confused all process. Here are my code:
    input code is not showing, not sure where I am missing,,,,

    thanks

    Contact.php

    <?php
    class Contact extends Page { ...}

    class Contact _Controller extends Page_Controller {
       static $allowed_actions = array(
          'ContactForm'
       );
       
       function ContactForm(){
       $fields = new FieldSet(
             new TextField('Name', 'name: *', '', '20'),   
             new EmailField('Email', 'email: *', '', '50')
          );
          $actions = new FieldSet(new FormAction('sendContactForm', 'Submit'));
          $validatior = new RequiredFields('Name’, 'Email');

          return new Form($this, 'ContactForm', $fields, $actions, $validatior);   
       }

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

       function SendContactForm($data, $form){ … }
    }
    ?>

    MyForm.php

    <?php
    class MyForm extends Form{
       function __construct($controller, $name){
          $fields = new FieldSet(
             new TextField('Name', 'name: *', 'first', '20'),   
             new EmailField('Email', 'email: *', '', '50')
          );
          $actions = new FieldSet(new FormAction('sendContactForm', 'Submit'));
          $validatior = new RequiredFields('FirstName', 'LastName', 'Email', 'Details');         
          parent::__construct($controller, $name, $fields, $actions, $validatior);
       }
       
       function forTemplate(){
          return $this->renderWith( array(
             $this->class,
             'Form'
          ));   
       }
       
       function sendContactForm($data, $form){ ...   }
    }   
    ?>

    Contact.ss


    <% include MyForm %>
    ...

    MyForm.ss

    <form $FormAttributes id="contactForm">
    <table class="contactTable" cellpadding="0" cellspacing="0">
    <tr><th> <table class="formLft" cellpadding="0" cellspacing="0">
    <tr><th><label>name <span>*</span></label></th>
    <td>
    <div class="inputField fltLft" style="margin-right:3px;">
    <div class="inputField2">
    <div class="inputField3">
    $dataFieldByName(Name)
    </div></div></div>
    <div class="clr"></div>
    </td> </tr>
    <tr><th><label for="$FormName_Email">email <span>*</span></label></th>
    <td><div class="inputField"><div class="inputField2"><div class="inputField3">
    $dataFieldByName(Email)
    </div></div></div></td>
    </tr></table>
    </form>

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: form template Link to this post

    Have you done a ?flush=all to include the new template? Also note your custom template should be in the 'Includes' directory of your theme.

  • leafchild
    Avatar
    Community Member
    41 Posts

    Re: form template Link to this post

    Hi Millr, thank you for the replay.

    I just did ?flush=all and also my custom template is already in 'Includes' directory
    but i still seen same issue.

    input tag is not display at MyForm.ss so must be something to do either Contact.php or MyForm.php, or both.

  • leafchild
    Avatar
    Community Member
    41 Posts

    Re: form template Link to this post

    Please someone HELP!! I am still struggling same issue.

    I found this page (http://pastie.org/522439) and copy and past most of the code to see if it would work.
    Unfortunately the issue is still same. "<input type ....>" is not display(text box is not displayed)
    What am I missing?

    Contact.php

    class Contact_Controller extends Page_Controller {
    function MyFirstForm(){
    return new MyForm($this, 'MyFirstForm');
    }
    }

    class MyFormSubmission extends DataObject {
    static $db = array(
    'FirstName' => 'Varchar(255)',
    'Email' => 'Text'
    );
    }

    MyForm.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="FirstName" class="field text">
    <label class="left" for="{$FormName}_FirstName">First name</label><br>
    $dataFieldByName(FirstName)
    </div>

    <div id="Email" class="field email">
    <label class="left" for="{$FormName}_Email">Email :</label><br>
    $dataFieldByName(Email)
    </div>

    $dataFieldByName(SecurityID)
    </fieldset>

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

    MyForm.php

    <?php
    //MyForm Sub Class, file name: MyForm.php
    class MyForm extends Form {

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

    $actions = new FieldSet(
    new FormAction('submit', 'Submit')
    );
    $validator = new RequiredFields('FirstName', 'Email');

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

    function forTemplate() {
    return $this->renderWith(array(
    $this->class,
    'Form'
    ));
    }

    function submit($data, $form) {
    // do stuff here
    }

    }

    ?>

  • Raver0124
    Avatar
    Community Member
    8 Posts

    Re: form template Link to this post

    I know its over 2 years since the first post but I had the same issue.
    It seems if I do this, it fixed it self

    function forTemplate() {
    return $this->renderWith(array( $this->class, 'Form' ));
    }

    to

    public function forTemplate() {
    return $this->renderWith(array( $this->class));
    }

    1512 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.