Jump to:

23460 Posts in 18907 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Class based Forms

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1102 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    Class based Forms Link to this post

    Hi folks,

    I've made a form in a class - saved in mysite/code/MyListingForm.php

    <?php
    class MyListingForm extends Form { //etc }

    this is all good - but how do I actually attach this form into a template page?
    the documentation only shows how to write the class...

    Cheers!

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Class based Forms Link to this post

    You need to have the method forTemplate() in your form class.

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

    Second, in your PageClass controller you need to call the form.

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

    Third, call your form in your page template. ($MyListingForm)
    Fourth, create a template for your form in templates/includes/MyListingForm.ss

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Class based Forms Link to this post

    You don't need to add the forTemplate method - it automatically tries to render with a template the same as the class name, or just the default form template.

    As for adding it to a controller, you do similar to what Devlin said, but you also should add the form to the controller's $allowed_actions definition:

    class PageType_Controller extends Page_Controller {

    public static $allowed_actions = array(
    'ListingForm'
    );

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

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: Class based Forms Link to this post

    Thanks folks - all working now

    Just to note: I also had to put the following in the form template:

    <input class="hidden nolabel" type="hidden" id="Form_Form_SecurityID" name="SecurityID" value="$securityID" />

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