Zend Framework comes with a set of validators related to Internationalization.
Zend\I18n\Validator\Float allows you to validate if a given value contains a floating-point value. This validator validates also localized input.
The following options are supported for Zend\I18n\Validator\Float:
The simplest way to validate a float is by using the system settings. When no option is used, the environment locale is used for validation:
1 2 3 4 5 | $validator = new Zend\I18n\Validator\Float();
$validator->isValid(1234.5); // returns true
$validator->isValid('10a01'); // returns false
$validator->isValid('1,234.5'); // returns true
|
In the above example we expected that our environment is set to “en” as locale.
Often it’s useful to be able to validate also localized values. Float values are often written different in other countries. For example using english you will write “1.5”. In german you may write “1,5” and in other languages you may use grouping.
Zend\I18n\Validator\Float is able to validate such notations. However,it is limited to the locale you set. See the following code:
1 2 3 4 5 | $validator = new Zend\I18n\Validator\Float(array('locale' => 'de'));
$validator->isValid(1234.5); // returns true
$validator->isValid("1 234,5"); // returns false
$validator->isValid("1.234"); // returns true
|
As you can see, by using a locale, your input is validated localized. Using a different notation you get a FALSE when the locale forces a different notation.
The locale can also be set afterwards by using setLocale() and retrieved by using getLocale().
Zend\I18n\Validator\Int validates if a given value is an integer. Also localized integer values are recognised and can be validated.
The following options are supported for Zend\I18n\Validator\Int:
The simplest way to validate an integer is by using the system settings. When no option is used, the environment locale is used for validation:
1 2 3 4 5 | $validator = new Zend\I18n\Validator\Int();
$validator->isValid(1234); // returns true
$validator->isValid(1234.5); // returns false
$validator->isValid('1,234'); // returns true
|
In the above example we expected that our environment is set to “en” as locale. As you can see in the third example also grouping is recognised.
Often it’s useful to be able to validate also localized values. Integer values are often written different in other countries. For example using english you can write “1234” or “1,234”. Both are integer values but the grouping is optional. In german for example you may write “1.234” and in french “1 234”.
Zend\I18n\Validator\Int is able to validate such notations. But it is limited to the locale you set. This means that it not simply strips off the separator, it validates if the correct separator is used. See the following code:
1 2 3 4 5 | $validator = new Zend\I18n\Validator\Int(array('locale' => 'de'));
$validator->isValid(1234); // returns true
$validator->isValid("1,234"); // returns false
$validator->isValid("1.234"); // returns true
|
As you can see, by using a locale, your input is validated localized. Using the english notation you get a FALSE when the locale forces a different notation.
The locale can also be set afterwards by using setLocale() and retrieved by using getLocale().
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.