E
E
Eugene Vdovenko2015-12-24 11:58:22
Zend Framework
Eugene Vdovenko, 2015-12-24 11:58:22

How to make the \Zend\Form\Element\Select field optional?

I set the form element like this:

$select = array();
foreach ($cFields["contents"] as $variant) {
    $select[$variant] = $variant;
}
$form->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => $field['name'],
        'options' => array(
            'label' => $field['title'],
            'empty_option' => 'Выбрать из списка',
            'value_options' => $select,
        ),
));

Next, when submitting:
...
$form = $this->getProfileForm();
$form->setData($this->request->getPost());
$form->setInputFilter(new \Zend\InputFilter\InputFilter());
if ($form->isValid()) {
...

At this point, validation fails, swears at selects: "The field is required and cannot be empty." If you set a value in selects - all the rules.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Mitrushchenkov, 2015-12-24
@Misanthropist

'required' => falseI'm only superficially familiar with the Zend Framework, but have you tried adding values ​​like 'allow_empty' => true?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question