Quick Start Zend Framework validation with Zend_Validate

Short Introduction to Zend_Validate

Zend Framework makes your life easier than you think, (my personal opinion)

To work with Zend_Validate there are two Primary  functions to use

1.isValid()

2.Zend_Validate::is()

3.getMessages()

Even Zend Framework is provided extra Secondary methods to make the life more easier

1.setMessage()

1.isValid()

performs validation check,returns True or false.If all validation clears it just returns true else return false

2.Zend_Validate::is()

Is a static method to check validation,It returns true or false depends on validation check.

3.getMessages()

Returns array of messages after validation, if you are using isValid() more than once,then setMessages() will return most recent call of isValid()’s erros and previous msg will be cleared.

One thing to note that, you can validate all the fields at a time if you are using Zend_Form and already defined validation for each html fields.You can check that in Zend_Validation with Zend_Form.

Following is a example of email validation,Zend already provided a email validation ,we can use that class to check validation.

1
2
3
4
5
6
7
8
9
10
11
12
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email))
{
   echo "Email id is valid";
}
else
{
   foreach ($validator->getMessages() as $messageId => $message)
   {
     echo "Validation failure '$messageId': $messagen";
   }
}

3.setMessage()

————————————-

setMessage() method with which you can specify the format of a message returned by getMessages() in case of validation failure.

It takes 2 Arguments

1st argument is the error msg to display and the second msg is the token.

You can even set multiple messages with setMessage()

Set Single Messge using setMessage()

1
2
3
4
5
6
7
8
9
10
11
$validator = new Zend_Validate_StringLength(8);
$validator->setMessage(
         'The string '%value%' is too short; it must be at least %min% ' .
          'characters',
          Zend_Validate_StringLength::TOO_SHORT);
if (!$validator->isValid('word'))
{
   $messages = $validator->getMessages();
   echo current($messages);
   // "The string 'word' is too short; it must be at least 8 characters"
}

Set Multiple Messge using setMessage()

1
2
3
4
5
6
7
8
9
10
11
12
13
$validator = new Zend_Validate_StringLength(array('min' => 8, 'max' => 12));
$validator->setMessages(
                       array(
                             Zend_Validate_StringLength::TOO_SHORT =>
                                       'The string '%value%' is too short',
                             Zend_Validate_StringLength::TOO_LONG  =>
                                        'The string '%value%' is too long'
                             ));
if (!$validator->isValid('word'))
{
   $messages = $validator->getMessages();
   echo current($messages);
}

 

How many way we can validate a input field using zend_Validate

Style-1

1
2
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email)){  }

Style-2

1
if (Zend_Validate::is($email, 'EmailAddress')) { }

How to check a input value between two numbers

if (Zend_Validate::is($value, 'Between', array('min' => 1, 'max' => 12))) {
 // Yes, $value is between 1 and 12
 }}