PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using the static is() method

If it's inconvenient to load a given validation class and create an instance of the validator, you can use the static method Zend_Validate::is() as an alternative invocation style. The first argument of this method is a data input value, that you would pass to the isValid() method. The second argument is a string, which corresponds to the basename of the validation class, relative to the Zend_Validate namespace. The is() method automatically loads the class, creates an instance, and applies the isValid() method to the data input.

<?php
if (Zend_Validate::is($email'EmailAddress')) {
    
// Yes, email appears to be valid
}

You can also pass an array of constructor arguments, if they are needed for the validator.

<?php
if (Zend_Validate::is($value'Between', array('min' => 1'max' => 12))) {
    
// Yes, $value is between 1 and 12
}

The is() method returns a boolean value, the same as the isValid() method. When using the static is() method, validation failure messages are not available.

The static usage can be convenient for invoking a validator ad hoc, but if you have the need to run a validator for multiple inputs, it's more efficient to use the non-static usage, creating an instance of the validator object and calling its isValid() method.

Also, the Zend_Filter_Input class allows you to instantiate and run multiple filter and validator classes on demand to process sets of input data. See Zend_Filter_Input.

Namespaces

When working with self defined validators you can give a fourth parameter to Zend_Validate::is() which is the namespace where your validator can be found.

<?php
if (Zend_Validate::is($value'MyValidator', array('min' => 1'max' => 12),
                      array(
'FirstNamespace''SecondNamespace')) {
    
// Yes, $value is ok
}

Zend_Validate allows also to set namespaces as default. This means that you can set them once in your bootstrap and have not to give them again for each call of Zend_Validate::is(). The following code snippet is identical to the above one.

<?php
Zend_Validate
::setDefaultNamespaces(array('FirstNamespace''SecondNamespace'));
if (
Zend_Validate::is($value'MyValidator', array('min' => 1'max' => 12)) {
    
// Yes, $value is ok
}

if (
Zend_Validate::is($value,
                      
'OtherValidator',
                      array(
'min' => 1'max' => 12)) {
    
// Yes, $value is ok
}

For your convenience there are following methods which allow the handling of namespaces:

  • Zend_Validate::getDefaultNamespaces(): Returns all set default namespaces as array.

  • Zend_Validate::setDefaultNamespaces(): Sets new default namespaces and overrides any previous set. It accepts either a string for a single namespace of an array for multiple namespaces.

  • Zend_Validate::addDefaultNamespaces(): Adds additional namespaces to already set ones. It accepts either a string for a single namespace of an array for multiple namespaces.

  • Zend_Validate::hasDefaultNamespaces(): Returns TRUE when one or more default namespaces are set, and FALSE when no default namespaces are set.

Zend Framework