Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » custom form

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

Page: 1
Go to End
Author Topic: 932 Views
  • Downing Media
    Avatar
    Community Member
    5 Posts

    custom form Link to this post

    Can somebody show me a complete example of a custom form and template? Does not have to be very complex. I had it working and messed something up. Trying to get a site finished and I am low on sleep

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: custom form Link to this post

    Welcome to the forums! Perhaps look at the example on http://doc.silverstripe.org/form#using_a_custom_template

  • Downing Media
    Avatar
    Community Member
    5 Posts

    Re: custom form Link to this post

    Thanks for the welcome and the quick reply.

    I used that the the link you provided the first time I had the form working. I deleted the wrong file and now I get get it setup again.

    Here is what I think I need to do but it is not working. Create a php file in the mysite directory for the MyForm class and then create the template in the Include directory. I will also need to create a formholder.php so I can create the page in the admin area.

    I am missing something but can't figure out what.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: custom form Link to this post

    Here is what I think I need to do but it is not working. Create a php file in the mysite directory for the MyForm class and then create the template in the Include directory. I will also need to create a formholder.php so I can create the page in the admin area.

    I think the template needs to be in templates/ not templates/includes/ since its not an 'include' per se. After following the steps on the wiki page do you get an error message or is it just a blank page?

  • Downing Media
    Avatar
    Community Member
    5 Posts

    Re: custom form Link to this post

    I was getting a blank page. Since the last post I added <% include MyForm %> and now I get the labels from the MyForm.ss but not the textboxes. I am not sure if adding this was correct but I am trying everything.

    Here is what I have

    /mysite/code/quoteform.php

    <?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')
    );

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

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

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

    }

    /mysite/code/quotepage.php

    <?php
    class QuotePage extends Page {

    static $db = array(
    );

    static $has_many = array(

    );
    }

    class QuotePage_Controller extends Page_Controller {

    }

    /themes/templates/mytemplate/quotepage.ss

    //code above is left out to condense//
                
             $Content

    <% include MyForm %>

    //code below is left out to condense//

    /themes/templates/mytemplate/Includes/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>
    $dataFieldByName(FirstName)
    </div>

    <div id="Email" class="field email">
    <label class="left" for="$FormName_Email">Email</label>
    $dataFieldByName(Email)
    </div>

    $dataFieldByName(SecurityID)
    </fieldset>

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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: custom form Link to this post

    You haven't told the controller about the form. You must create an instance of your MyForm class to pass it to the template.

    class QuotePage_Controller extends Page_Controller {

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

    Then in the template use $MyForm.

  • Downing Media
    Avatar
    Community Member
    5 Posts

    Re: custom form Link to this post

    Worked great.

    Thanks for the fast response.

  • dayer
    Avatar
    Community Member
    11 Posts

    Re: custom form Link to this post

    Hi,

    for blackcandy theme, for example, I must to change in MyForm.ss:

    <div id="FirstName" class="field text">
    <label class="left" for="{$FormName}_FirstName">First name</label>
    </div>


    for:

    <div id="FirstName" class="field text">
    <label class="left" for="{$FormName}_FirstName">First name</label>
    <div class="middleColumn">$dataFieldByName(FirstName)</div>
    </div>

    Thanks.

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