Page 1 of 1

Zend validator help???

Posted: Fri Jun 26, 2009 12:21 pm
by alex.barylski
I have the following validation code for a username:

Code: Select all

     $v = new Zend_Validate();
      $v->addValidator(new Zend_Validate_StringLength(6, 12))
        ->addValidator(new Zend_Validate_Alnum());
 
      if(!$v->isValid($user_name)){
        $buffer = implode('<br />', $v->getMessages());
        throw new Exception("<br />$buffer");
      }
Works great except it doesn't include the name of the field so the error message looks like:
'' cannot be less than 6 characters
Where the '' would contain the contents of the variable itself I would like to prefix that message with the field name so people knew what was going on???

Cheers,
Alex

Re: Zend validator help???

Posted: Fri Jun 26, 2009 2:16 pm
by requinix
You're getting the design mixed up.

The validator doesn't validate form fields, it validates data. It has no concept of a form field or a file name or anything like that. You feed it data, it tells you whether the data is valid.
If you want that kind of functionality you need Zend_Form.

Code: Select all

if(!$v->isValid($user_name)){
You're validating a username, right? One specific field? It says so right in the code.
So change the error message to reflect that, like

Code: Select all

throw new Exception("The username:<br />$buffer");

Re: Zend validator help???

Posted: Fri Jun 26, 2009 2:35 pm
by alex.barylski
You're validating a username, right? One specific field?
I'm actually validating about 12 fields but I totally missed that...I can just prepend the field name in the message, groovy thanks :)