Looking at the code of Zend_Validate_Abstract a bit more, it seems that I can't have exactly what I propose unless Zend_Filter_Input is being modified as well, because Zend_Validate_Abstract right now does not know about other fields than the one it's currently processing.

So, there's two possibilities to achieve this:

a) the hard way - modify Zend_Filter_Input (ZFI) to allow passing of
   ZFI-instances to the validator objects, so that these can query the
   values of other fields in the input array.
   This would allow validation across fields like I proposed in my
   previous mail.

b) the easy way - just add a very simple validator called "Equals" and
   inject the other field's value at validator definition time, like so:

   $f = array();
   $v = 'password' => array(
           array('Equals', $_POST['password2')
         );
   $input = new Zend_Filter_Input($f, $v, $_POST);
   if ($input->hasInvalid()) {
       echo "Uh-oh, the password fields do not contain the same value!";
   }

Solution b) would do fine for my purposes alone, but it feels kinda ugly.

What would you others think/recommend?

CU
 Markus

Markus Wolff schrieb:
Hey Jason,

that's where I was looking and couldn't find any. Sure, I can write my own validator, I just couldn't believe I was the first one finding the obvious gap in the existing validators.

What I need is a validator that compares one field's value to another. Something along the lines of:

$f = array();
$v = 'password' => array(
        array('SameAsField', 'password2')
      );
$input = new Zend_Filter_Input($f, $v, $_POST);
if ($input->hasInvalid()) {
    echo "Uh-oh, the password fields do not contain the same value!";
}

Now, is the field "password" does not have the same value as the field "password2", then an error message is shown.

Am I the only one finding this useful? If not, how does one go about adding validators? Is a proposal needed or should I just send the code to the package maintainer?

CU
 Markus

Jason Qi schrieb:
Hi Markus,

Do you think the Standard Validation Classes is useful
for you?
http://framework.zend.com/manual/en/zend.validate.set.html

Or you might write Validators yourself to fit your
needs. see here:

http://framework.zend.com/manual/en/zend.validate.writing_validators.html

Regards,

Jason.

--- Markus Wolff <[EMAIL PROTECTED]> wrote:

Hi there,

quite often one has to make a form where a user can
create a user account. It's customary to add two password fields,
and add a validation rule so that when the entries in both fields do not
match, an error message is presented to the user.

I was searching for such a validation rule for use
with Zend_Filter_Input, but couldn't find any. Am I
missing something? Or would you guys do it entirely differently?

CU
  Markus


Reply via email to