I haven't really thought about it in detail, but I imagine it would act like
a SWITCH statement, usage might be something like this:

 

$validator = new Zend_Validate_Branch(array(

1 => array('notempty'),

));

$validator->isValid($options, $choice);

 

I am not sure whether $choice1 can be passed in isValid() or whether it
needs to be in the constructor.

 

In this example the array('notempty') is any validator chain form, in this
case I've used the short version for Zend_Form, but ofcourse it could be an
object, or even another branch.

 

Additionally you will notice there is nothing for 2, this would mean no
additional validation.

 

There may need to be something like:

 

$validator = new Zend_Validate_Branch(array(

1 => array('notempty'),

2 => 1,

));

 

This could allow 2 to inherit the same chain as 1.

 

Looking at the original email I also wonder whether there may be need for a
validator that applies to checkbox groups. As described there is a case for
"at least one" so a minimum and maximum item count validator.

 

Cheers,
Steven

 

From: Hector Virgen [mailto:[email protected]] 
Sent: Thursday, 24 December 2009 2:40 AM
To: Steven Brown
Cc: Guillaume ORIOL; [email protected]
Subject: Re: [fw-general] isValid not executed on multicheckbox when the
element is not required and value is not set

 

There's already support for validating against other form fields, but you
have to write your own validator to do it:

 

class My_Validator implements Zend_Validate_Interface

{

    public function isValid($value, $context = null)

    {

        /* $context is array of all form values */

    }

}


Zend_Validate_Branch sounds like a way to simplify this. How do you suppose
it'd work?


--
Hector



On Tue, Dec 22, 2009 at 5:23 PM, Steven Brown <[email protected]> wrote:

I think this calls for a new component Zend_Validate_Branch that would allow
the validator chain to branch depending on values of other form fields.
Anyone agree?

 

From: Hector Virgen [mailto:[email protected]] 
Sent: Wednesday, 23 December 2009 2:59 AM
To: Guillaume ORIOL
Cc: [email protected]
Subject: Re: [fw-general] isValid not executed on multicheckbox when the
element is not required and value is not set

 

Place the validator at the choice1 element. You can write a custom validator
that accepts a $context array as the second parameter to filter() which will
contain the rest of the forms values. You can then test the checkboxes at
that point.

--
Hector

On Tue, Dec 22, 2009 at 5:54 AM, Guillaume ORIOL <[email protected]> wrote:

Here is a schematic view of the form I have:
(*) choice 1
   [X] option 1
   [_] option 2
   [_] option 3
( ) choice 2

When "choice 1" is checked, at least one of the three options should be
checked.
I wanted to write a specific validator for the options but...
as the "options" multi-checkbox is not required (as they are ignored if
choice 2 is checked), when no option is checked, the validator is not
executed at all.

Does anybody have a solution for it?

Thanks
-- 

Guillaume ORIOL

 

 

Reply via email to