Zend validator help???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Zend validator help???

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Zend validator help???

Post 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");
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Zend validator help???

Post 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 :)
Post Reply