7
votes

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?!

4
Does $numeration have an empty element? Do a debug to be sure.Jason McCreary
nope, array numeration is created just before i add first element in ituser198003
Does the $numeration array actually have an element 0?Leo

4 Answers

11
votes

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));
11
votes

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));
3
votes

This works under 2.3:

$options = array('0'=>'Zero','1'=>'One');
echo $this->Form->select('field-name',$options,array('empty'=>false));
0
votes

I know this is an old question, but, if you are having trouble with CakePHP 1.2 (yes, I know it sucks to use such an old version), the correct way is:

$form->select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

http://book.cakephp.org/1.2/en/The-Manual/Core-Helpers/Form.html#select