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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Dependant dropdown menu's (Forms)


Reply


574 Views

Avatar
borriej

Community Member, 267 Posts

28 December 2011 at 4:09am

Edited: 28/12/2011 8:37am

Hello

How do you make a dependant dropdown menu on a Form?

i have a bookingsform:
- first dropdown menu returns a DataObject. Titles of pages can be selected.
- Now a secondary dropdown menu must be filled with possible dates/prices, which are stored in the DataObject 'DatesPrices'

How do i do this?

my first dropdown is generated by:

class Tour_Controller extends Page_Controller {

   public function getMyTours(){
      $today = date('Y-m-d');
      if($Pages = DataObject::get("Tour", "EndDate >= '$today'","StartDate", "", "" )){
         return $Pages->map('Title', 'Title', 'Please Select');
      }else{
         return array('No Tours found');
      }
   }

class Tour_Controller extends Page_Controller {

   function BookingsForm() {
      $fields = new FieldSet(
         
         new DropdownField(
            'TourProgram',
            'Select a trip',
             $this->getMyTours()
         ),   

Dates and prices are stored in:

class DatesPrice extends DataObject {
   public static $db = array(
      'Periode' => 'Text',
      'Price' => 'Int',
      'Comment' => 'Text',
      'SingleSupplement' => 'Int',
      'Description' => 'HTMLText',
   );
   public static $has_one = array (
      'Tour' => 'Tour'
   );
   public function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push(new TextField('Periode'));
      $fields->push(new TextField('Price'));
      $fields->push(new TextField('Comment'));
      $fields->push(new TextField('SingleSupplement'));
      $fields->push(new SimpleTinyMCEField('Description'));
      
      return $fields;
   }
}

i need the second form to check which value is selected on the first dropdown.. and then use that as some kind of filter on dataobject DatesPrices, but how?