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:

Data Model Questions /

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

Generate array for GroupedDropdownField

Go to End

2 Posts   2187 Views


Community Member, 12 Posts

16 December 2009 at 7:14am

Edited: 16/12/2009 5:02pm


Hoping someone can help me... I need to generate an array of arrays in order to populate a GroupedDropdownField on a form.

I have an TrekPage class that lets users add multiple TrekDates via a DataObjectManager, each with a StartDate and EndDate field. I'd love to be able to write a function that iterates through the available TrekPages and creates an array for each consisting of dates, each of which is the result of a function that determines the range of the TrekDate's StartDate & EndDate (like this

i know i can use things like $this->toArray(); ( but haven't had much luck so far. And sorry if the above isn't explained very clearly, I'm new to a lot of this..



Community Member, 12 Posts

16 December 2009 at 5:01pm

Edited: 16/12/2009 6:25pm

Ok this is what I'm up to so far

1. creates a form with new GroupedDropdownField
2. function makes an array of arrays for each TrekPage (event page) consisting dates returned by the DateRange function. This array is inserted into the third parameter of the GroupedDropdownField.
3. on I have $BookingForm which should display a <select> drop-down with dates organised into <optgroups> for each TrekPage.


function BookingForm() {
		return new Form($this, 'BookingForm', new FieldSet(
			new GroupedDropdownField(



        function getTreksArray() {
			$trekPage = DataObject::get('TrekPage');
			$treksArray = array();
			foreach($trekPage as $trek) {
				$trekData = array();
				foreach($trek->TrekDate as $trekDate) {
					$dateRange = DateRange($trekDate);
				return $trekData;
			return $treksArray;
	function DateRange($date) {
		$start = new Date();
		if (!is_null($this->EndDate)) { // Check if there is an end date
		   $end = new Date();
		   return $start->RangeString($end);
		} else {
		   return $start->Long();