Getting the error - "The input was not found in the haystack"

When I use Element\Select::class in my Form I get this error - “The input was not found in the haystack”.
I get this error even when I don not use any validator in my form validation.

Elements like Select, Email, Checkbox, DateTimeSelect, etc. already come with pre-configured input filter specifications, like filters and validators. Therefore, you will get this error message if a value other than the one defined in the value options is selected.

See also in the documentation of the Select element:

This element adds an InArray validator to its input filter specification in order to validate on the server if the selected value belongs to the values.

What exactly you mean when you say ‘casting forms to business objects…’. I am using the form, as follows:

$statusForm = new StatusForm($statusTypes);

statusTypes could be any of [‘submitted’ => ‘Submitted’, ‘inprogress’ => ‘In Progress’,‘resolved’ => ‘Resolved’]

Please elaborate. Thx.

I’m sorry, but what does that have to do with the current topic?

I tried to show how I am using the form and does it match with your opinion - ‘casting froms to business objects…’.

Please create a new thread for this because it has nothing to do with the current topic "Getting the error - “The input was not found in the haystack”.
Thanks!

My Apologies for not having been able to convey my understanding. Anyways, can you please give example when you say : “Validate and display simple and complex forms, casting forms to business objects and vice versa.”.

Also I have been using Select at 2 different places in the code. At one place it don’t give ‘InArray’ error while at the other it does give error. And I haven’t found any difference between the 2.Thanks!

I did not say that because it is the description of laminas-form.

And again please open a new forums thread for this.

Please remember that I am not a magician or anything like that. I cannot see your code and therefore do not know what you are trying to do and what your two forms look like.

I disabled the InArray validation, as follows and it worked!

$statusElement = $this->get('status-types');
$statusElement->setDisableInArrayValidator(true);

The pointers to the Laminas documentation were helpful. But I am not sure if this is the best solution?

This may work, but on the other hand, the input value cannot be compared with the predefined values (value_options) for the select field. This can lead to unexpected input values.

I could successfully set the status and confirmed it via looking at the value stored in the database. Nonetheless, please advise about the best solution. Thx.

This is the standard way to define the value options for a Select element:

final class Example extends Laminas\Form\Form
{
    public function init(): void
    {
        $this->add(
            [
                'name'    => 'genre',
                'type'    => Laminas\Form\Element\Select::class,
                'options' => [
                    'value_options' => [
                        'pop'   => 'Pop',
                        'blues' => 'Blues',
                        'jazz'  => 'Jazz',
                    ],
                ],
            ]
        );
    }
}

Initialise the form, set data and validate it:

$form = new Example();
$form->init();

$form->setData(['genre' => 'jazz']);
var_dump($form->isValid()); // true

$form->setData(['genre' => 'foo']);
var_dump($form->isValid()); // false
var_dump($form->getMessages()['genre']); // ['notInArray' => 'The input was not found in the haystack']

Still the same error with this approach also. The only way is

$statusElement->setDisableInArrayValidator(true);

Please provide your form, otherwise no one knows what you are doing and how to help you.