Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend: Form validation: value was not found in the haystack error

I have a form with 2 selects. Based on the value of the first select, it updates the values of the second select using AJAX. Doing this makes the form not being valid. So, I made the next change:

        $form=$this->getAddTaskForm(); //the form

        if(!$form->isValid($_POST)) {
            $values=$form->getValues();

            //get the options and put them in $options

                $assignMilestone=$form->getElement('assignedMilestone');
                $assignMilestone->addMultiOptions($options);

        }

        if($form->isValid($_POST)) {
               //save in the database
            }else {
               //redisplay the form
            }  

Basically, I check if it is valid and it isn't if the user changed the value of the first select. I get the options that populated the second select and populate the form with them. Then I try to validate it again. However this doesn't work. Anybody can explain why? The same "value was not found in the haystack" is present.

like image 536
Andrew Avatar asked Jun 27 '12 17:06

Andrew


3 Answers

You could try to deactivate the validator:

in your Form.php

$field = $this->createElement('select', 'fieldname');
$field->setLabel('Second SELECT');
$field->setRegisterInArrayValidator(false);
$this->addElement($field);

The third line will deactivate the validator and it should work.

like image 85
Joel Lord Avatar answered Oct 14 '22 22:10

Joel Lord


You can also disable the InArray validator using 'disable_inarray_validator' => true:

For example:

    $this->add( array(
        'name'     => 'progressStatus',
        'type'     => 'DoctrineModule\Form\Element\ObjectSelect',
        'options' => array(
            'disable_inarray_validator' => true,
        ),

    )); 
like image 13
Onshop Avatar answered Oct 14 '22 21:10

Onshop


Additionaly you should add you own InArray Validator in order to protect your db etc.

In Zend Framework 1 it looks like this:

$this->addElement('select', $name, array(
            'required' => true,
            'label' => 'Choose sth:',
            'filters' => array('StringTrim', 'StripTags'),
            'multiOptions' => $nestedArrayOptions,
            'validators' => array(
                array(
                    'InArray', true, array(
                        'haystack' => $flatArrayOptionsKeys,
                        'messages' => array(
                            Zend_Validate_InArray::NOT_IN_ARRAY => "Value not found"
                        )
                    )
                )
            )
        ));

Where $nestedArrayOptions is you multiOptions and $flatArrayOptionsKeys contains you all keys.

like image 3
Bolebor Avatar answered Oct 14 '22 22:10

Bolebor