Search code examples
phpzend-framework2

Zend Framework 2: Select element - empty option


Why the choice of an empty item in Zend\Form\Element\Select is selected also the zero element?

In form class:

$this->add(array(
'type' => 'Zend\Form\Element\Select',
    'name' => 'active',
    'options' => array(
        'label' => _('Active'),
        'empty_option' => '',
        'value_options' => array(
            1 => 'Yes',
            0 => 'No',
    ),
 ),
));

In controller:

$searchForm->setData( $request->getPost() );

html, generated ZF2:

<select name="active">
    <option value="" selected="selected"></option>
    <option value="1">Yes</option>
    <option value="0" selected="selected">No</option>
</select>

Solution

  • This pretty much has to do with how PHP interprets '', 0 and null in array()-context. I'm not sure if this can even be worked out giving the current implementation, but i suggest you open an issue on github about this.

    This could be considered a bug.