40.2. Standard Validation Classes

The Zend Framework comes with a standard set of validation classes, which are ready for you to use.

40.2.1. Alnum

Returns true if and only if $value contains only alphabetic and digit characters. This validator includes an option to also consider white space characters as valid.

40.2.2. Alpha

Returns true if and only if $value contains only alphabetic characters. This validator includes an option to also consider white space characters as valid.

40.2.3. Barcode

This validator is instantiated with a barcode type against which you wish to validate a barcode value. It currently supports "UPC-A" (Universal Product Code) and "EAN-13" (European Article Number) barcode types, and the isValid() method returns true if and only if the input successfully validates against the barcode validation algorithm. You should remove all characters other than the digits zero through nine (0-9) from the input value before passing it on to the validator.

40.2.4. Between

Returns true if and only if $value is between the minimum and maximum boundary values. The comparison is inclusive by default ($value may equal a boundary value), though this may be overridden in order to do a strict comparison, where $value must be strictly greater than the minimum and strictly less than the maximum.

40.2.5. Ccnum

Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum) for credit card numbers.

40.2.6. Date

Returns true if $value is a valid date of the format YYYY-MM-DD. If the optional locale option is set then the date will be validated according to the set locale. And if the optional format option is set this format is used for the validation. For details about the optional parameters see Zend_Date::isDate().

40.2.7. Digits

Returns true if and only if $value only contains digit characters.

40.2.8. EmailAddress

Zend_Validate_EmailAddress allows you to validate an email address. The validator first splits the email address on local-part @ hostname and attempts to match these against known specifications for email addresses and hostnames.

Basic usage

A basic example of usage is below:

<?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";
    }
}
        

This will match the email address $email and on failure populate $validator->getMessages() with useful error messages.

Complex local parts

Zend_Validate_EmailAddress will match any valid email address according to RFC2822. For example, valid emails include bob@domain.com, bob+jones@domain.us, "bob@jones"@domain.com and "bob jones"@domain.com

Some obsolete email formats will not currently validate (e.g. carriage returns or a "\" character in an email address).

Validating different types of hostnames

The hostname part of an email address is validated against Zend_Validate_Hostname. By default only DNS hostnames of the form domain.com are accepted, though if you wish you can accept IP addresses and Local hostnames too.

To do this you need to instantiate Zend_Validate_EmailAddress passing a parameter to indicate the type of hostnames you want to accept. More details are included in Zend_Validate_Hostname, though an example of how to accept both DNS and Local hostnames appears below:

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

Checking if the hostname actually accepts email

Just because an email address is in the correct format, it doesn't necessarily mean that email address actually exists. To help solve this problem, you can use MX validation to check whether an MX (email) entry exists in the DNS record for the email's hostname. This tells you that the hostname accepts email, but doesn't tell you the exact email address itself is valid.

MX checking is not enabled by default and at this time is only supported by UNIX platforms. To enable MX checking you can pass a second parameter to the Zend_Validate_EmailAddress constructor.

<?php
$validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS, true);
        

Alternatively you can either pass true or false to $validator->setValidateMx() to enable or disable MX validation.

By enabling this setting network functions will be used to check for the presence of an MX record on the hostname of the email address you wish to validate. Please be aware this will likely slow your script down.

Validating International Domains Names

Zend_Validate_EmailAddress will also match international characters that exist in some domains. This is known as International Domain Name (IDN) support. This is enabled by default, though you can disable this by changing the setting via the internal Zend_Validate_Hostname object that exists within Zend_Validate_EmailAddress.

<?php
$validator->hostnameValidator->setValidateIdn(false);
        

More information on the usage of setValidateIdn() appears in the Zend_Validate_Hostname documentation.

Please note IDNs are only validated if you allow DNS hostnames to be validated.

Validating Top Level Domains

By default a hostname will be checked against a list of known TLDs. This is enabled by default, though you can disable this by changing the setting via the internal Zend_Validate_Hostname object that exists within Zend_Validate_EmailAddress.

<?php
$validator->hostnameValidator->setValidateTld(false);
        

More information on the usage of setValidateTld() appears in the Zend_Validate_Hostname documentation.

Please note TLDs are only validated if you allow DNS hostnames to be validated.

40.2.9. Float

Returns true if and only if $value is a floating-point value.

40.2.10. GreaterThan

Returns true if and only if $value is greater than the minimum boundary.

40.2.11. Hex

Returns true if and only if $value contains only hexadecimal digit characters.

40.2.12. Hostname

Zend_Validate_Hostname allows you to validate a hostname against a set of known specifications. It is possible to check for three different types of hostnames: a DNS Hostname (i.e. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e. localhost). By default only DNS hostnames are matched.

Basic usage

A basic example of usage is below:

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

This will match the hostname $hostname and on failure populate $validator->getMessages() with useful error messages.

Validating different types of hostnames

You may find you also want to match IP addresses, Local hostnames, or a combination of all allowed types. This can be done by passing a parameter to Zend_Validate_Hostname when you instantiate it. The paramter should be an integer which determines what types of hostnames are allowed. You are encouraged to use the Zend_Validate_Hostname constants to do this.

The Zend_Validate_Hostname constants are: ALLOW_DNS to allow only DNS hostnames, ALLOW_IP to allow IP addresses, ALLOW_LOCAL to allow local network names, and ALLOW_ALL to allow all three types. To just check for IP addresses you can use the example below:

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

As well as using ALLOW_ALL to accept all hostnames types you can combine these types to allow for combinations. For example, to accept DNS and Local hostnames instantiate your Zend_Validate_Hostname object as so:

<?php
$validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP);
        

Validating International Domains Names

Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support international characters in domain names. These are known as International Domain Names (IDN). These domains can be matched by Zend_Validate_Hostname via extended characters that are used in the validation process.

At present the list of supported ccTLDs include:

  • at (Austria)

  • ch (Switzerland)

  • li (Liechtenstein)

  • de (Germany)

  • fi (Finland)

  • hu (Hungary)

  • no (Norway)

  • se (Sweden)

To match an IDN domain it's as simple as just using the standard Hostname validator since IDN matching is enabled by default. If you wish to disable IDN validation this can be done by by either passing a parameter to the Zend_Validate_Hostname constructor or via the $validator->setValidateIdn() method.

You can disable IDN validation by passing a second parameter to the Zend_Validate_Hostname constructor in the following way.

<?php
$validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, false);
        

Alternatively you can either pass TRUE or FALSE to $validator->setValidateIdn() to enable or disable IDN validation. If you are trying to match an IDN hostname which isn't currently supported it is likely it will fail validation if it has any international characters in it. Where a ccTLD file doesn't exist in Zend/Validate/Hostname specifying the additional characters a normal hostname validation is performed.

Please note IDNs are only validated if you allow DNS hostnames to be validated.

Validating Top Level Domains

By default a hostname will be checked against a list of known TLDs. If this functionality is not required it can be disabled in much the same way as disabling IDN support. You can disable TLD validation by passing a third parameter to the Zend_Validate_Hostname constructor. In the example below we are supporting IDN validation via the second parameter.

<?php
$validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, true, false);
        

Alternatively you can either pass TRUE or FALSE to $validator->setValidateTld() to enable or disable TLD validation.

Please note TLDs are only validated if you allow DNS hostnames to be validated.

40.2.13. InArray

Returns true if and only if a "needle" $value is contained in a "haystack" array. If the strict option is true, then the type of $value is also checked.

40.2.14. Int

Returns true if and only if $value is a valid integer.

40.2.15. Ip

Returns true if and only if $value is a valid IP address.

40.2.16. LessThan

Returns true if and only if $value is less than the maximum boundary.

40.2.17. NotEmpty

Returns true if and only if $value is not an empty value.

40.2.18. Regex

Returns true if and only if $value matches against a regular expression pattern.

40.2.19. StringLength

Returns true if and only if the string length of $value is at least a minimum and no greater than a maximum (when the max option is not null). Since version 1.5.0, the setMin() method throws an exception if the minimum length is set to a value greater than the set maximum length, and the setMax() method throws an exception if the maximum length is set to a value less than than the set minimum length. Since version 1.0.2, this class supports UTF-8 and other character encodings, based on the current value of iconv.internal_encoding.

    Поддержать сайт на родительском проекте КГБ