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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Class based Forms


Go to End


4 Posts   1471 Views

Avatar
JonShutt

Community Member, 244 Posts

4 February 2011 at 6:39pm

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

Community Member, 344 Posts

4 February 2011 at 10:32pm

Edited: 04/02/2011 10:32pm

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

Community Member, 244 Posts

4 February 2011 at 11:39pm

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
JonShutt

Community Member, 244 Posts

5 February 2011 at 8:35am

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