Search code examples
phpformszend-frameworkzf3

Getting value from input select Zend Framework 3


how I can get value from input select in ZF3?

$education = $form->get('education');
$education->setValueOptions([
     '1' =>'option 1',
     '2' => 'option 2',
]);

returns integer value 1,2, not 'option 1' or 'option 2'

Even if I remove index and leave code like below

$education->setValueOptions([
     'option 1',
     'option 2',
]);

it doesn't work and returns the same as above.

But if I modify code like this

$education->setValueOptions([
     'option 1' => 'anything'
     'option 2' => 'anything'
]);

it returns correct values as 'option 1' or 'option 2'.

Is is correct, or I'm dooing something wrong?


Solution

  • You're thinking it wrong. For example:

    $education->setValueOptions([
         'array_key' => 'array_value'
    ]);
    

    In PHP the value of this array would be array_value while array_key would be the key. The logic in zend is the opposite when it is translated to the front-end. In the front end the array_key would be the <option> value while the array_value would be the <option> label.

    The above code would be translated to the following in the front-end

    <option value="array_key">array_value</option>