Rajesh Kumar
Rajesh Kumar

Reputation: 21

Multi select dropdown CakePHP 3 selected issue

$selected_country = array(1,3);

$this->Form->input('country', [
    'options' => $source_types,
    'label' => 'Country: ',
    'multiple' => true,
    'class' => ' form-control',
    'selected' => $selected_country,
    'type' => 'select'
]);

If selected country has only one value then it selects the option but if the selected country has more than one value then it doesn't select any value.

Upvotes: 1

Views: 4153

Answers (2)

gabrielkolbe
gabrielkolbe

Reputation: 145

For anyone this works echo $this->Form->input('venues._ids', ['options' => $venues, 'class'=>'form-control select4', 'label' => false]); IF you have the database associations set up AND you have it referenced in the controller like so

$event = $this->Events->get($id, [
            'contain' => ['Venues']
        ]);

otherwise adding the 'default' => [1, 2] ids in also works but it has to be first created in the controller and then fed to the view in list form.

Upvotes: 1

Rayann Nayran
Rayann Nayran

Reputation: 1135

If you want to pass more than one value on $selected_country, try it:

echo $this->Form->select('rooms', [
    'multiple' => true,
    // options with values 1 and 3 will be selected as default
    'default' => [1, 3]
]);

Reference: CakePHP Cookbook

Upvotes: 3

Related Questions