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

后端 未结 4 2012
情歌与酒
情歌与酒 2021-02-13 03:45

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 mad

相关标签:
4条回答
  • 2021-02-13 03:56

    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,
            ),
    
        )); 
    
    0 讨论(0)
  • 2021-02-13 03:59

    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.

    0 讨论(0)
  • 2021-02-13 04:01

    You may also add options to select element before checking for the form validation. This way you are insured the select value is in range.

    0 讨论(0)
  • 2021-02-13 04:14

    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.

    0 讨论(0)
提交回复
热议问题