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 Validators with Zend_File_Transfer

Putting validators to work is quite simple. There are several methods for adding and manipulating validators:

  • isValid($files = null): Checks the specified files using all validators. $files may be either a real filename, the element's name or the name of the temporary file.

  • addValidator($validator, $breakChainOnFailure, $options = null, $files = null): Adds the specified validator to the validator stack (optionally only to the file(s) specified). $validator may be either an actual validator instance or a short name specifying the validator type (e.g., 'Count').

  • addValidators(array $validators, $files = null): Adds the specified validators to the stack of validators. Each entry may be either a validator type/options pair or an array with the key 'validator' specifying the validator. All other options will be considered validator options for instantiation.

  • setValidators(array $validators, $files = null): Overwrites any existing validators with the validators specified. The validators should follow the syntax for addValidators().

  • hasValidator($name): Indicates whether a validator has been registered.

  • getValidator($name): Returns a previously registered validator.

  • getValidators($files = null): Returns registered validators. If $files is specified, returns validators for that particular file or set of files.

  • removeValidator($name): Removes a previously registered validator.

  • clearValidators(): Clears all registered validators.

Example 407. Add Validators to a File Transfer Object

<?php
$upload 
= new Zend_File_Transfer();

// Set a file size with 20000 bytes
$upload->addValidator('Size'false20000);

// Set a file size with 20 bytes minimum and 20000 bytes maximum
$upload->addValidator('Size'false, array('min' => 20'max' => 20000));

// Set a file size with 20 bytes minimum and 20000 bytes maximum and
// a file count in one step
$upload->setValidators(array(
    
'Size'  => array('min' => 20'max' => 20000),
    
'Count' => array('min' => 1'max' => 3),
));

Example 408. Limit Validators to Single Files

addValidator(), addValidators(), and setValidators() each accept a final $files argument. This argument can be used to specify a particular file or array of files on which to set the given validator.

<?php
$upload 
= new Zend_File_Transfer();

// Set a file size with 20000 bytes and limits it only to 'file2'
$upload->addValidator('Size'false20000'file2');

Normally, you should use the addValidators() method, which can be called multiple times.

Example 409. Add Multiple Validators

Often it's simpler just to call addValidator() multiple times with one call for each validator. This also increases readability and makes your code more maintainable. All methods provide a fluent interface, so you can couple the calls as shown below:

<?php
$upload 
= new Zend_File_Transfer();

// Set a file size with 20000 bytes
$upload->addValidator('Size'false20000)
       ->
addValidator('Count'false2)
       ->
addValidator('Filessize'false25000);

Note

Note that setting the same validator multiple times is allowed, but doing so can lead to issues when using different options for the same validator.

Last but not least, you can simply check the files using isValid().

Example 410. Validate the Files

isValid() accepts the file name of the uploaded or downloaded file, the temporary file name and or the name of the form element. If no parameter or null is given all files will be validated

<?php
$upload 
= new Zend_File_Transfer();

// Set a file size with 20000 bytes
$upload->addValidator('Size'false20000)
       ->
addValidator('Count'false2)
       ->
addValidator('Filessize'false25000);

if (!
$upload->isValid()) {
    print 
"Validation failure";
}

Note

Note that isValid() will be called automatically when you receive the files and have not called it previously.

When validation has failed it is a good idea to get information about the problems found. To get this information, you can use the methods getMessages() which returns all validation messages as array, getErrors() which returns all error codes, and hasErrors() which returns TRUE as soon as a validation error has been found.

Zend Framework