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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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