11.2. Error Notification

The Xyster_Validate_Errors class is a notification object. It is responsible for collecting the messages returned by a Zend_Validate class. It implements Xyster_Collection_Interface so you can iterate over it and count the elements inside.

Each message is stored in the set as a Xyster_Validate_Error object, containing the message and the name of the field to which the message applies.

11.2.1. Example of Use

The notification object can accept either a Zend_Filter_Input or a Zend_Validate_Interface that will be used to collect messages.

Here is an example with a single Zend_Validate_Interface object.

<?php
require_once 'Zend/Validate/EmailAddress.php';
require_once 'Xyster/Validate/Errors.php';

$fieldName = 'email';
$errors = new Xyster_Validate_Errors();
$validator = new Zend_Validate_EmailAddress();

$email = $values[ $fieldName ];

if ($validator->isValid($email)) {
    // email appears to be valid
} else {
    // email is invalid; get messages
    $errors->addValidateMessages($validator, $fieldName);
}

Here is an example with a Zend_Filter_Input object.

<?php
require_once 'Zend/Filter/Input.php';
require_once 'Xyster/Validate/Errors.php';

$errors = new Xyster_Validate_Errors();
$filters = array(
    'month'   => 'Digits',
    'account' => 'StringTrim'
);
$validators = array(
    'account' => 'Alpha'
);

$input = new Zend_Filter_Input($filters, $validators, $values);

if ( $input->isValid() ) {
    // all inputs are valid
} else {
    // something is invalid; get messages
    $errors->addFilterInputMessages($input);
}

Using the notification pattern, you can pass a Xyster_Validate_Errors object to the layer of your application that performs the validation so the messages can find their way to the presentation layer.