Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Class based Forms


Reply

4 Posts   1114 Views

Avatar
Futureweb

4 February 2011 at 6:39pm Community Member, 233 Posts

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!

Avatar
Devlin

4 February 2011 at 10:32pm (Last edited: 4 February 2011 10:32pm), Community Member, 215 Posts

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

Avatar
ajshort

4 February 2011 at 11:39pm Community Member, 244 Posts

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

Avatar
Futureweb

5 February 2011 at 8:35am Community Member, 233 Posts

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" />