21288 Posts in 5733 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 958 Views |
-
Class based Forms

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!
-
Re: Class based Forms

4 February 2011 at 10:32pm Last edited: 4 February 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 -
Re: Class based Forms

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');
} -
Re: Class based Forms

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" />
| 958 Views | ||
|
Page:
1
|
Go to Top |



