Sunday
2
May
2010

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

Back to top
Saturday
3
April
2010

Using countries for localization

Hy interested ones,

I did not find a correct heading for what I want to describe you today.
But there is a new feature which has been implemented by me so let’s see some details:

Most of you are already using Zend_Locale. Give a language and get a locale in return.
You used this for translation, currency, date or other purposes.

Now the idea for this new feature is simple… why should a user not be able to give a country instead of a language to get a locale in return. For example “austria”… when I give “AT” then I could expect that the locale “de_AT” is being used.

And exactly this feature is available as with ZF 1.10.4 or ZF 2.0.

Of course there are much other usecases where this feature could be used.
For example, when your user selects a flag which represents his country. Then you could use only the region/country to get a locale with language in return.

$locale = new Zend_Locale('US');
// returns "en_US"

Or think of currencies. You could just give the country and get the currency for this country.

$currency = new Zend_Currency(array('locale' => 'AT'));
// uses "EUR" which is the currency for the locale "de_AT"

Generally you can use this feature everywhere within the whole framework where you used only languages or locales before.

But of course there is one thing to note:
You have to uppercase countries when you want to use them. The reason is that there are countries which are equal to languages. For example “om”… you could mean the country Oman and expect “ar_OM” as locale, or you could mean the language “Oromo” and expect “or_KE” as locale.

When you find this feature useful, feel free to use it yourself. :-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Saturday
27
March
2010

Zend_Translate goes configuration

Hy interested ones,

This is my third entry for Zend_Translate this week. Maybe I should call it “Zend_Translate“-week. :-)

Ok… so what have I done this time.
I reworked Zend_Translate a little bit. Now it supports Zend_Config and additionally all options can be given as array.

The change seems small but makes coding a little bit easier and more comfortable.
Previously you had to do:

new Zend_Translate('csv', '\path\to\myfile.csv', null, array('delimiter' => ':'));

Now you can do:

new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => '\path\to\myfile.csv',
        'delimiter' => ':'
    )
);

By using the array syntax the code is much more readable and seems more logical.
Of course you can also use a Zend_Config object instead of an array.

This syntax is also supported by using addTranslation(), but as you call addTranslation() on the adapter, you can’t change the adapter within this method so it will be ignored in that case.

$translate->addTranslation(
    array(
        'content' => '\path\to\translations\',
        'locale'    => 'de',
        'scan'      => Zend_Translate::LOCALE_DIRECTORY
    )
);

Note that this new feature is available as with ZF 1.10.3.
Of course the old syntax is still supported. So your old code works without that it has to be reworked (when you don’t like to do that).

When you find this feature useful, feel free to use it yourself. :-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top