2014-01-22 4 views
3

Я новичок в cakephp. Мне нужно добавить по умолчаниюДобавление опции по умолчанию «Пожалуйста, выберите» в выпадающем списке cakephp

<option value="0">--Please Select--</option> 

в моем следующем поле выбора:

$attributes = array("empty"=>false,"Selected" => 'Select City',"id" => "location"); 
echo $form->select("gal_location_id", $gal_locations,null,$attributes); 

Я пытался добавить

$gal_locations[0] = "--Select City--"; 
$attributes = array("empty"=>false,"default" => 0,"id" => "location"); 

но вариант приходя в нижней части списка. Каким образом можно добавить параметр по умолчанию?

ответ

12

Вы ищете «пустой» атрибут:

$this->Form->input('gal_location_id', array(
    'type' => 'select', 
    'options' => $gal_locations, 
    'empty' => 'Select City', // <-- Shows as the first item and has no value 
    'id' => 'location' 
)); 
0

Смотрите эквивалент в CakePHP3 от this post

CakePHP3

С следующим вариантом вы не получите уведомление выбрать выпадающий пункт.

Так что, если вы просто ищете значение по умолчанию и заставляете пользователя выбирать другой, просто замените строку «пустой».

echo $this->Form->input('db_field', [ 
    'label' => 'My Drop Down', 
    'empty' => [$default => $default], //Your default options 
    'options' => $my_options //Your array /list' 
]); 
0
echo $this->Form->input('country_id',[ 
    `enter code here`'options' =>$country, 
    'label' => false, 
    'class'=>'form-control select2', 
    'empty'=> 'Select...', 
    'value' => '' 
]); 
Смежные вопросы