K
K
karenshahmuradyan2017-11-27 14:51:07
Zend Framework
karenshahmuradyan, 2017-11-27 14:51:07

Only the last value of setValueOptions is added?

only the last value of setValueOptions is added;

{
                    "type": "select",
                    "label": "Select",
                    "className": "form-control",
                    "name": "select-1511596175604",
                    "values": [
                        {
                            "label": "Option 1",
                            "value": "option-1",
                            "selected": true
                        },
                        {
                            "label": "Option 2",
                            "value": "option-2"
                        },
                        {
                            "label": "Option 3",
                            "value": "option-3"
                        }
                    ]
                }

switch ($element->type) {

                case 'select':
                    $selectElement = new Select($element->name);
                    $selectElement->setAttribute('class', $element->className);


                    foreach ($element->values as $option) {
                        $options = array(
                            $option->label => $option->value
                        );

                         $selectElement->setValueOptions($options);
                    }

                    $form->add($selectElement);
                    break;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pozdnyakov, 2018-01-15
@Afinogen

Because you overwrite it on every iteration of the loop

$options = array();
foreach ($element->values as $option) {
     $options[$option->label] = $option->value;
 }
$selectElement->setValueOptions($options);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question