Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Generate array for GroupedDropdownField

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

Page: 1
Go to End
Author Topic: 1520 Views
  • JackAttack
    Community Member
    12 Posts

    Generate array for GroupedDropdownField Link to this post


    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 http://silverstripe.org/template-questions/show/267739#post267739).

    i know i can use things like $this->toArray(); (http://doc.silverstripe.org/doku.php?id=dataobjectset) 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..


  • JackAttack
    Community Member
    12 Posts

    Re: Generate array for GroupedDropdownField Link to this post

    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 BookingPage.ss 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();

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.