0

I want to disable the validation on one of the zend form element based on the input on another element on the same form. I need to achieve this using javascript/jquery. Something very common but very surprisingly couldn't find it over the internet.

e.g.

In the controller:

$oFormMassEdit = new Account_Form_Returns_Return();
$this->view->form = $oFormMassEdit;

In Account_Form_Returns_Return's constructor:

$oNoteElement = new Zend_Form_Element_Textarea('option', array(
                                                'required'  => true,
                                  ));
$this->addElemet($oNoteElement)

$oReasonElement = new Zend_Form_Element_Select('note', array(
            'multiOptions'  => array (
                'return'  => 'return',
                'defect'  => 'Kaput',
                'other'   => 'Anderer Grund'
            ),
            'required'      => true,
                    ));
$this->addElement($oReasonElement);

$this->addDisplayGroup(array('note', 'option'), 'main', array('legend' => 'Retouren'));

$this->addElement('button','send', array(
           'type'   => 'submit',
           'label'  => 'Methode speichern',
        ));

and finally in the view,

<?= $this->form; ?>
2
  • Can you show us some code.. Commented Aug 8, 2013 at 8:41
  • So, I have edited my question to show you how to create a zend form and then render it on the view... Commented Aug 8, 2013 at 8:56

1 Answer 1

1

Javascript can't (not in a sensible way) switch Zend_Form configuration. What you can do is changing the 'required' param for certain form fields on validation. For example; If you want to allow fieldTwo to be empty if fieldOne has 'desiredValue' as value you can achieve this using the following function in your form:

public function isValid($data) {
    if('desiredValue' == $data['fieldOne'])) {
        $this->getElement('fieldTwo')->setRequired(false);
    }

    return parent::isValid($data);
}   
Sign up to request clarification or add additional context in comments.

4 Comments

ok, the problem is that I get some post data from page A and then based on this post data, I construct page B. On page B, I display a form, this form is based on post data got from page A. Now page C validates the form created on page B and if it is invalidated, user should get back to page B with highlighted invalid fields.
To keep it simple use action B as renderer of form and validation of it. You can post values of the POST of page A to the form of page B and use them in the form to add/remove fields, change validators etc.
Rather than remove the validator in question, you could conditionally add it in $form->isValid() before calling parent::isValid(). See this post from Jeremy Kendall for one approach to dynamically changing the form behavior (adding fields, in his case) based on actions that take place on the client-side. He uses a preValidate() method, but you could probably just override isValid() as described above, which keeps the controller-flow pretty standard.
I agree with this possibility. Whatever suits your situation at best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.