PC.
PC.

Reputation: 132

Radio button in Zend Framework 2 working fine with one option but not the other

My problem is pretty strange as i have a radio button in my form with two options Yes and No, with No being the default choice. The form is connected to a database and the input is stored, whats weird is that when i am submitting the form with second choice which is "yes" in this case the form works fine and input is stored but whenever i am going for the first option it gives me an error saying

The input was not found in the haystack

Now, here is one more thing, this radio button is copy-paste of another radio button i have in the form with different options, which is working fine, although it provided me the same error at first but suddenly it started working and is still working fine.

Here is my code for the form element:

$this->add(array(
        'name' => 'handicapped',
        'options' => array(
            'label' => 'Handicapped',
            'value_options' => array(
                array(
                    'value' => '0',
                    'label' => 'No',
                    'selected' => true,
                ),
                array(
                    'value' => '1',
                    'label' => 'Yes',
                ),
            ),
            'label_attributes' => array(
                'class' => 'radio-inline',
            ),
        ),
        'type'  => 'Radio',
    ));

here is the filter:

$this->add(array(
         'name' => 'handicapped',
         'required' => true,
         'validators' => array(
            array(
                'name'    => 'InArray',
                 'options' => array(
                     'haystack' => array('0','1')
                ),
            ),
        ),
     ));

and here is the view:

   echo $this->formElement($applicant->get('handicapped')); 
   echo $this->formElementerrors($applicant->get('handicapped')); 

Problem is strange and i couldn't find anything on it so any help is appreciated.

EDIT: While I was trying to make it work, i tried it with SELECT instead of RADIO and it worked but i need it to work with RADIO. Just sharing in case, it provides some help for the answer.

Upvotes: 0

Views: 998

Answers (1)

PC.
PC.

Reputation: 132

I found the reason for this strange problem, it was the stupid javascript i used to prefill the form, it was messing with the inputs and providing the wrong data at the time of posting.

Disabled the script and got the code working perfectly. Just writing this as an answer cause question should not be left unanswered and SO recommends against deleting the questions.

Upvotes: 1

Related Questions