I have an issue in my code which I can't resolve. I'm using Zend framework 2.4 and I wrote a form but as soon as I validate it, I got the error The input was not found in the haystack. This are the 3 input where I got the error:
$this->add(array(
'name' => 'ACTIVITE1',
'type' => 'Zend\Form\Element\Select',
'required' => 'required',
'options' => array(
'value_options' => array(
'Choisir l\'activité'
),
'disable_inarray_validator' => false,
),
'attributes' => array(
'class' => 'form-control',
'id' => 'select-session1'
)
));
$this->add(array(
'name' => 'ACTIVITE2',
'type' => 'Zend\Form\Element\Select',
'required' => 'required',
'options' => array(
'value_options' => array(
'Choisir l\'activité'
)
),
'disable_inarray_validator' => false,
'attributes' => array(
'class' => 'form-control',
'id' => 'select-session2'
)
));
$this->add(array(
'name' => 'ACTIVITE3',
'type' => 'Zend\Form\Element\Select',
'required' => 'required',
'options' => array(
'value_options' => array(
'Choisir l\'activité'
)
),
'disable_inarray_validator' => false,
'attributes' => array(
'class' => 'form-control',
'id' => 'select-session3'
)
));
I saw in other form that I should put 'disable_inarray_validator' => false
but this doesn't work too.
Of course it's not working.
That message comes from the
\Zend\Validator\InArray
and essentially, it means: "your user is doing something hacky with your select, pay attention".An exemple would be a Select
Preferred fruit
with two options, like "Banana" and "Ananas", but the user "hacks" the select and sends to the server the value "Audi". TheInArray
validator is really important and shouldn't be disabled (well, only in a few exceptions..).Now, why are you getting this error? The answer is... You didn't told what are the select options. You created a Select, but you didn't specified what are its options. You put the label/placeholder at the place of the options. A correct Select would be:
What is "weird" is the fact that you are filling something in an empty select, and my question then is: why?
Selects are (typically) there for a predefined list of values. If you want to allow your users to fill a custom text, then you should consider to create a
Text
field with the autocomplete option.Edit: select with values from DB
If you want to create a select with a list of options that come from the database, the route is a bit more complex, but once you've learned how to do it, it will become way easier.
PAY ATTENTION: this will not be a "copy&paste solution". Since I'm not having access to your code, I'm making up names (classes, namespaces, methods, variables) just to create a complete example :)
First off, you must create a custom element. In this case, it will be a custom select:
What is really important here is that you must instantiate your element (options, classes, and so on..) inside
init
method.Since this element has a dependency (
ActivityMapper
), you'll have to create a factory for this element:This factory must be added in the configuration, more precisely, inside
module.config.php
:Now, you must modify your form too. All elements must be added to form inside
init
method, and not inside the constructor:Finally, you'll have to modify your controller too, because you need to retrieve the form from the
FormElementManager
:A next nice step would be to create a controller plugin for the
$formManager
, instead of making it as a dependency of each controller, but this is a different problem..