Zend_Form_Element_Multiselect :: ‘121’ was not found in haystack

Hello Friends,

While working with Zend_Form, i came accross a problem that realy made me to pull up my hair.
I was simply using Zend_Form_Element_Select to show drop down menue in form. I got following message when i submited my form :

‘121’ was not found in haystack

Apprently this message was because of some pre-set ‘validator’ as i enabled no validator for this select box. Following code i used to generate select box that generated above message:

$pCatg = array(121,123);
$arrList = array("name1"=>"name1", "name2"=>"name2", "name3"=>"name3");
$catg = new Zend_Form_Element_Multiselect('category_id');
$catg->setLabel('Category')
->setMultiOptions($arrList)
->setValue($pCatg)
->setDecorators($this->decorator);

Following Html code was generated by above code:

<select name=”test” id=”test”>
<option value=”121″ label=”name1″>name1</option>
<option value=”122″ label=”name2″>name2</option>
<option value=”123″ label=”name3″>name3</option>
</select>

Reason of Problem :

As you can see above that i haven’t used any Validator for this drop down menu, so that brought the problem. Actualy InArray validator is enabled automatically if validator was not present

Solution of Problem :

$pCatg = array(121,123);
$arrList = array(“name1″=>”name1”, “name2″=>”name2”, “name3″=>”name3”);
$catg = new Zend_Form_Element_Multiselect(‘category_id’);
$catg->setLabel(‘Category’)
->setMultiOptions($arrList)
->setValue($pCatg)
->setRegisterInArrayValidator(false)
->setDecorators($this->decorator);

So,
solution was just to set ‘setRegisterInArrayValidator(false)’,

Hope it may help someone,
Thanks

Advertisements

46 thoughts on “Zend_Form_Element_Multiselect :: ‘121’ was not found in haystack

  1. Faheem Abbas says:

    I was facing the same problem, and it make me scratch my head several times.
    This article help me in getting things done.
    Thanks a lot.

  2. Ezequiel M. G. says:

    Hi there!

    I had the same problem, and i tried to patch that problem somehow but i luckly found this post of you and it was great!

    You help me a lot!

    Thanks you man! =)

  3. E says:

    Would not simply using Zend_Form_Element_select instead of Zend_Form_Element_Multiselect solve the problem?

    Zend_Form_Element_Multiselect expects an array since it allows for multiple selections while Zend_Form_Element_select a string value.

  4. Visitor says:

    Thanks man!
    You used an dynamic options array for the select element. So basically the validator complains that something was passed to the forms target php which is not in the options array (any more).
    So another solution is to populate the select element prior to validation.

    pseudo:
    $model = My_Supercool_Model_Something();
    $form = $model->get_form();
    $form->populate($model->get_form_select_options());
    if($form->isValid())
    {
    // here you go
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s