Zend_Validate

Validation of two values or how to proove identical-ness

Hy interested ones,

today I integrated two improvements for Zend_Validate_Identical.

Zend_Validate_Identical is a validator which enables you to check if two values are identical.
Values means in this case string, integers, floats, or even objects.

A manual validation would look like this:

$valid = new Zend_Validate_Identical(array('token' => '1234'));

if ($valid->isValid($input)) {
    // let's go on
} else {
    // oops... not valid
}

Simple as is…

Now, when $input is the string “1234” we will get true. When it differs we will get false. This means even if we have a integer “1234” the validation will fail. The reason behind this behaviour is that Zend_Validate_Identical does a strict validation including the type of the input.

But sometimes it is wished and necessary to validate only the content regardless of it’s type.
Zend_Validate_Identical supports now also non-strict validation. See the following example:

$valid = new Zend_Validate_Identical(
    array('token' => '1234', 'strict' => false)
);

if ($valid->isValid($input)) {
    // let's go on
} else {
    // oops... not valid
}

As you can see the above example is nearly identical to the first example with one difference. We defined the property strict to be false. By using this option we said Zend_Validate_Identical to use non-strict validation.

In this case, when $input is a integer “1234” we will also get true in return and also when it’s a float “1234”.

Now what to do when you want to validate if two form elements are identical. Seems tricky as Zend_Validate_Identical has no connection to Zend_Form, and it would not know which two elements you want to validate.

Easy as is, you can now give the elements name as token which holds the token to validate against. What does this mean? Let’s see a little example:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../library');
require_once('Zend/Loader/Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
print "<pre>";

$request = new Zend_Controller_Request_Http();

// setup the form
$form = new Zend_Form();
$form->setMethod(Zend_Form::METHOD_POST)
$form->addElement('password', 'password1');
$form->addElement('password', 'password2', array(
    'validators' => array(
        array('identical', false, array('token' => 'password1'))
    )
));
$form->addElement('submit', 'submit');

// check the form
if($request->isPost()) {
    $formData = $request->getPost();
    if($form->isValid($formData)) {
        $form->getValues();
        echo "FORM VALID";
    } else {
        print "\nVALIDATION FAILURE:";
        print_r($form->getMessages());
    }
}
print "</pre>";
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php echo $form->render(new Zend_View());?>
</body>
</html>

You can run the above example standalone in your browser. It shows you the behaviour of Zend_Validate_Identical and how to combine it efficent within Zend_Form.

For us the important line is:

$form->addElement('password', 'password1');
$form->addElement('password', 'password2', array(
    'validators' => array(
        array('Identical', false, array('token' => 'password1'))
    )
));

As you see we added the Identical validator to the second element by using the first elements name as token.
This way the value of the first element is compared with the value of the second element.

Simple as is :-)

This is handy when you want to validate two user inputs. For example when your user has to enter his email adress two times to be sure he did not mistype it, or he has to enter the same password two times.

Note that these two described features are available within trunk and as with ZF 1.10.5 and NOT BELOW.

I hope you find this two features useful. More to come soon…

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Author:
  • Share/Bookmark

Thomas Weidner’s Blog: Translating validation messages

Thomas Weidner revealed another “new” feature for Zend Framework 1.10 (expected to be released tomorrow). The framework comes now with translated validation messages.

As you know the returned error messages can be changed. This is very important for sites which are not delivered in english. But they must translate all messages into their own language. This is a very tendious task.

Follow to his post and read how to use pre-translated validation messages directly on your website and how to add them to your translations repository.

  • Share/Bookmark

Thomas Weidner’s Blog: New Zend_Validate_NotEmpty implementation

Thomas Weidner contributed small but very important change to Zend_Validate_NotEmpty – compatibility with PHP’s empty() function.

Today I added a completly new implementation for the “not empty” validator.Zend_Validate_NotEmpty worked in past only the “framework” way. It detected whitespaces as empty and excluded ‘0′. This collidates with how PHP’s empty() works.

It looks like not much, but should make use of this validator more natural and will definitely limit a number of stupid bugs in everyone’s applications (remember PHP4 and PHP5 inconsistencies?).

  • Share/Bookmark

Thomas Weidner’s Blog: Validating barcodes

Zend Framework’s barcodes validator has been recently re-engineered by Thomas Weidner.

You think it did already exist? Yes, and no. Let’s clearify a little bit.
The old implementation of Zend_Validate_Barcode did not work properly. It was fixed to EAN13 and UPCA and very limited.

I reworked it completly, added new features and a easy API.
It is now a base component for Zend_Barcode, a new component which provides you with the ability to print barcodes.

New validator brings not only API changes towards greater flexibility, but also 12 new standards and improved validation.

  • Share/Bookmark

Thomas Weidner’s Blog: Task updates

There is new post on Thomas Weidner’s blog, where he talks about he’s recent contribution to Zend_Framework. Zend_Framework 1.10 should be shipped with his new Zend_Filter_Boolean, new version of Zend_Validate_Barcode and Zend_View_Helper_Currency. Thomas is also working on I18N webinar.

So I am preparing some I18n webinars… the first will cover Basics of I18n, usage and internals of translation. Its mainly for beginners and people who want to get knowledge on workflow and some internals. Of course it will also cover some examples. When I am receive good responses on this webinar I will also cover other I18n themes.

  • Share/Bookmark