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.

Form Questions /

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

generate multiple forms or fields on a page


Go to End


2457 Views

Avatar
keeny

Community Member, 48 Posts

20 February 2009 at 5:42pm

Hi guys,

I want to generate a list of fields or forms in a loop like so:

LocationHolder.ss

<ul id="Locations">
<% control Locations %>
	<li>
		$Title $courseDropDown
	</li>
<% end_control %>
</ul>

Here I am looping through Location models and calling Location.courseDropDown() to return a form for each location.

A form needs to be associated with a controller, so I have to define the function courseDropDown() twice. Once in the model and once the the controller, like so:

LocationPage.php

class LocationPage extends Page {

	function courseDropDown() {
		[...]
		return new Form($this, 'courseDropDown', $fields, $actions);	
	}
   
}

class LocationPage_Controller extends Page_Controller {
	
	function selectCourse($data, $form) {				
		Director::redirect('confirm');
	}
	
	function courseDropDown() {
		[...]
		return new Form($this, 'courseDropDown', $fields, $actions);	
	}
	
}

This seems to be a horrendous way to do things. Should I be generating the form in the LocationHolder instead? Something like...

LocationHolder.php

class LocationHolder extends Page {
	[...]
}

class LocationHolder_Controller extends Page_Controller {

	function formForAllChildren() {
		$location_holder = DataObject::get_one("LocationHolder");
		$locations = DataObject::get("LocationPage", "ParentID = $location_holder->ID", "", "", "");

		//for each location
			//build form
		//end

		return new Form();
	}

}

Any advice or pointers appreciated.

Thanks,

Barry.