Chapter 27. Zend_Validate
Prev |  | Next

## Chapter 27. Zend_Validate

## 27.1. Introduction

The Zend_Validate component provides a set of commonly needed validators. It also provides a simple validator chaining mechanism by which multiple validators may be applied to a single datum in a user-defined order.

What is a validator?

A validator examines its input with respect to some requirements and produces a boolean result - whether the input successfully validates against the requirements. If the input does not meet the requirements, a validator may additionally provide information about which requirement(s) the input does not meet.

For example, a web application might require that a username be between six and twelve characters in length and may only contain alphanumeric characters. A validator can be used for ensuring that usernames meet these requirements. If a chosen username does not meet one or both of the requirements, it would be useful to know which of the requirements the username fails to meet.

Having defined validation in this way provides the foundation for `Zend_Validate_Interface`, which defines two methods, `isValid()` and `getMessages()`. The `isValid()` method performs validation upon the provided value, returning `true` if and only if the value passes against the validation criteria. If `isValid()` returns `false`, the `getMessages()` is provided to return an array of messages explaining the reason(s) for validation failure.

| Note
The `getMessages()` returns validation failure messages for the most recent `isValid()` call only, since `isValid()` clears any messages existing from a previous `isValid()` call.

The following example illustrates validation of an e-mail address:

    <?php
    require_once 'Zend/Validate/EmailAddress.php';
    $validator = new Zend_Validate_EmailAddress();
    if ($validator->isValid($email)) {
        // email appears to be valid
    } else {
        // email is invalid; print the reasons
        foreach ($validator->getMessages() as $message) {
            echo "$message\n";
        }
    }

Prev |  | Next
Chapter 26. Zend_Uri | Home | 27.2. Validator Chains