Re: Quickform2 chaining rules

From: Date: Tue, 06 Dec 2011 08:35:55 +0000
Subject: Re: Quickform2 chaining rules
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-31327@lists.php.net to get a copy of this message
Hi Mark, On 06.12.2011 8:35, Mark Steudel wrote:
I have the following elements: $fieldset->addElement( 'checkbox', 'optin', array( 'value' => '1' ) )->setLabel( 'Newsletter check-in' ); $fieldset->addElement( 'text', 'parent_email' )->setLabel( 'Parent Email' ); And what I would like if the user checks the optin box, the parent_email input is required. I can't quite get my head around these chaining rules on different elements.
Your first step can probably be describing the validation scenario in plain words. E.g. "either the box is unchecked or email should be entered" or "Email is only needed if the box is checked". Then you "translate" that to $checkbox->addRule('empty') // box is unchecked
         ->or_($text->createRule('nonempty', 'Give email if you want newsletter')); // email is entered
or $checkbox->addRule('nonempty')
         ->and_($text->createRule('nonempty', 'Give email if you want newsletter'));
Note that if you don't give an error message to a rule, then the element it validates will not be considered invalid whatever the result. Thus errors will only be on the text field, never on the checkbox in these examples.

« previous php.pear.general (#31327) next »