Patrick Lorber wrote:
> 
> Hi all,
> 
> I have a form that collects billing and shipping information.  I want to
> be able to control whether or not the shipping information is required
> based on a same as shipping checkbox element.  I have written a validator
> using the $context array to check whether or not this checkbox is checked. 
> If the check box is checked, then the element is allowed to be blank. 
> Otherwise, it is not.
> 
> The problem is that the other validators in the chain continue to run when
> my custom validator returns true.  Since I have other validators that are
> needed when the element is not blank this presents a problem.  
> 
> I'm hoping I missed something here and that the group might be able to
> point me in the direction of how to handle this Use Case simply
> 
> Regards
> 

Ok, It was pretty simple...  I just checked the value of the checkbox in the
controller and then set the elements that needed to have their required flag
toggled to true:

        // make sure it is a post submission
        if (!$this->_request->isPost()){
                return $this->_helper->redirector('order');
        }
        
        $form = $this->getForm();
        $checked = ((int)$this->_request->getPost('sameAsShipping'));
        if ($checked !== 1){
                $form->getElement('contactNameFirst')->setRequired();
                $form->getElement('contactNameLast')->setRequired();
                $form->getElement('contactPhoneNumber')->setRequired();
                $form->getElement('contactAddress1')->setRequired();
        }
        
        // Validate Form
        if (!$form->isValid($this->_request->getPost())){
                $this->view->form = $form;
                return $this->render('index'); 
        }

In the Form Model, these Elements were added without setting required to
true.

Now I can Make My Own validator similar to 'NotEmpty' (i.e 'FieldRequired')
set the validators 'breakChainOnFailure' property to true, make sure this
validator is on the top of the stack, set the elements
'_autoInsertNotEmptyValidator' property to false. and problem solved.

Hope this can help someone else

-- 
View this message in context: 
http://www.nabble.com/validator-break-chain-on-success-tp21296665p21323359.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to