Zend Framework 2: Selecting Options as "Selected" in a Picklist

I am trying to install the second product as selected in the list, but the code below is not working. Any ideas. Thanks to

$this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'manufacturer',
        'options' => array(
            'label' => 'Manufacturer name',
            'value_options' => $this->getManufacturer(),
            'empty_option'  => '--- select manufacturer ---',
        ),
        'attributes' => array(
            'value' => 2,
            'selected' => true,
        ),
    ));

      

+3


source to share


2 answers


I gave a simple example here, hope this can help you. As you mentioned to get the selected element, just use the value attribute that has the value of that selected element, for example:

    $this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'gender',
        'options' => array(
            'label' => 'Gender',
            'value_options' => array(
                '1' => 'Select your gender',
                '2' => 'Female',
                '3' => 'Male'
            ),
        ),
        'attributes' => array(
            'value' => '1' //set selected to '1'
        )
    ));

      

you can use this link more And if you get



the haystack option is required

then add the disable_inarray_validator parameter to the parameters:

    $this->add(array(
    ...
    'options' => array(
      'disable_inarray_validator' => true,
      'label' => 'county',
    ),
 ));

      

+4


source


If I am not mistaken, the value is part of the options, not an attribute.



$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'manufacturer',
    'options' => array(
        'label' => 'Manufacturer name',
        'value_options' => $this->getManufacturer(),
        'empty_option'  => '--- select manufacturer ---',
        'value' => '2'
    )
));

      

0


source







All Articles