Question

I am new to cakephp. I need to add a default

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

in my following select field :

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

I tried to add

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

but the option coming at the bottom of the list. What is the proper way to add the default option ?

Was it helpful?

Solution

You're looking for the "empty" attribute:

$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'
));

OTHER TIPS

See the equivalent in CakePHP3 from this post

CakePHP3

With the following option you dont get a notification to choose a dropdown item.

So if you're just looking for a default value and force the user to pick another, just replace the assign a string to 'empty'.

echo $this->Form->input('db_field', [
  'label' => 'My Drop Down',
  'empty' => [$default => $default], //Your default options
  'options' => $my_options //Your array /list'
]);
echo $this->Form->input('country_id',[
    `enter code here`'options' =>$country,
    'label' => false,
    'class'=>'form-control select2',
    'empty'=> 'Select...',
    'value' => ''
]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top