Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the first item in select menu always empty?

Tags:

select

cakephp

Using CakePHP, I created select-option form element with:

echo $form->select('items', $numeration , array('selected' => 0));

It creates selection box, but the first option is always empty.

How can I get rid of that empty option? I did not manage to do anything with showEmpty option...

please help.... :-((

UPDATED:

cakephp code

echo $form->select('myOptions', array(1 => 'a', 2 => 'b', 3 => 'c'), array('empty'=>false));

creates next html:

<select id="myOptions" name="data[myOptions]">
<option selected="selected" value=""></option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>

what is wrong, and why do i have empty element?!

like image 968
user198003 Avatar asked Jul 25 '10 17:07

user198003


3 Answers

It's better to use:

$this->Form->input('items', array('options'=>$numeration));

By default it's without empty element. but to force it fully use

$this->Form->input('items', array('empty'=>false, 'options'=>$numeration));
like image 186
Nik Chankov Avatar answered Nov 03 '22 14:11

Nik Chankov


According to the docs the third argument is the default item to be selected. If you don't want an empty option to appear change your code to:

echo $form->select('items', $numeration , NULL, array('empty' => false));
like image 32
Sunny Avatar answered Nov 03 '22 13:11

Sunny


This works under 2.3:

$options = array('0'=>'Zero','1'=>'One');
echo $this->Form->select('field-name',$options,array('empty'=>false));
like image 3
tgurske Avatar answered Nov 03 '22 14:11

tgurske