Introduction to Zend Framework

 Learning Zend Framework

appendix

 Zend Framework Reference


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    75.3. Validator Chains

    Often multiple validations should be applied to some value in a particular order. The following code demonstrates a way to solve the example from the introduction, where a username must be between 6 and 12 alphanumeric characters:

    // Create a validator chain and add validators to it
    $validatorChain = new Zend_Validate();
    $validatorChain->addValidator(
                        new 
    Zend_Validate_StringLength(array('min' => 6,
                                                             
    'max' => 12)))
                   ->
    addValidator(new Zend_Validate_Alnum());

    // Validate the username
    if ($validatorChain->isValid($username)) {
        
    // username passed validation
    } else {
        
    // username failed validation; print reasons
        
    foreach ($validatorChain->getMessages() as $message) {
            echo 
    "$message\n";
        }
    }

    Validators are run in the order they were added to Zend_Validate. In the above example, the username is first checked to ensure that its length is between 6 and 12 characters, and then it is checked to ensure that it contains only alphanumeric characters. The second validation, for alphanumeric characters, is performed regardless of whether the first validation, for length between 6 and 12 characters, succeeds. This means that if both validations fail, getMessages() will return failure messages from both validators.

    In some cases it makes sense to have a validator break the chain if its validation process fails. Zend_Validate supports such use cases with the second parameter to the addValidator() method. By setting $breakChainOnFailure to TRUE, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation. If the above example were written as follows, then the alphanumeric validation would not occur if the string length validation fails:

    $validatorChain->addValidator(
                        new 
    Zend_Validate_StringLength(array('min' => 6,
                                                             
    'max' => 12)),
                        
    true)
                   ->
    addValidator(new Zend_Validate_Alnum());

    Any object that implements Zend_Validate_Interface may be used in a validator chain.

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...