SoFunction
Updated on 2025-03-10

php extension Zend Framework framework—Validate extension

I wrote an article about how to extend the Zend Framework framework under version 0.6 of the Zend Framework framework. This article should be said to be a similar article, but the environment has been replaced by the Zend Framework framework version 1.0RC1.

Before starting the Zend Framework framework extension, we recommend taking a look at some command specifications in the Zend Framework framework manual (recommended to use the Zend Framework framework). At the same time, we hope that readers will have a good understanding of the Zend Framework framework. If not, you can first go to PHPCHIAN's Zend Framework framework version to learn more, or go to phpeye to find relevant information.

The validator of the Zend Framework framework provides powerful verification functions, but it is still too tedious in actual operations. For example, the code of verification email is used to use the Zend Framework framework is as follows

<?php 

require_once 'Zend/Validate/'; 
$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"; 
    } 

?> 

Have you found it? It is still very similar to the verification method of not using the Zend Framework framework. But the Zend Framework framework helps us encapsulate the details of email verification. So how do we simplify this effect? (The following is my extended call method)

<?php 
$validate = new Phpbean_Validate(); 
        $validate -> set_breakOnFailure(false); 
$validate -> add('email',new Zend_Validate_EmailAddress(),'The email address is incorrect!');
$validate -> add('username',new Zend_Validate_StringLength(3,15),'The username length must be between 3 and 15!\'%value%\'The condition does not meet');
$validate -> add('password',new Zend_Validate_StringLength(6,20),'Password length must be between 6 and 20!');
$validate -> add('password',new Phpbean_Validate_isEqual($_POST['repassword']),'The password input does not match twice');
        $authcode = new Phpbean_Img_Code(); 
$validate -> add('yanxue8_authcode',new Phpbean_Validate_isEqual($authcode->authcode($_POST['yanxue8_authcode_mdcode'],'DECODE')),'Verification code does not match!');
        if( !$validate -> validator($_POST) ){ 
error_page('Register failed',$validate->getMessageText());
        } 
?> 

Using the above method on the one hand, the code is clear, and on the other hand, it is also beneficial to the agreed error handling. So how to do this?
The key is the Phpbean_Validate class.
In fact, it is very simple to implement. The Phpbean_Validate::add() method is to add verification rules one by one. Then call Phpbean_Validate::validator() to verify it is OK.
The specific implementation steps are as follows:
First, add a phpbean folder to the simultaneous directory of zend, and then add a file inside.
Then, add the definition of Phpbean_Validate class to the file. Note (you can modify it to your own file name and path name, but be sure to be consistent with the class name).
Here, I give the implementation process of my Phpbean_Validate class for reference only.

<? 
class Phpbean_Validate{ 
     
    protected $_fileds =array(); 
     
    protected $_message = array(); 
     
    protected $_breakOnFailure = true; 
     
    public function set_breakOnFailure($value){ 
        $this->_breakOnFailure = $value; 
    } 
     
    public function add($key,$validate,$message='',$breakOnFailure=''){ 
        if( empty($breakOnFailure) ) $breakOnFailure = $this->_breakOnFailure;  
        $this->_fileds[] = array($key,$validate,$message,$breakOnFailure); 
        return $this; 
    } 
     
    public function validator($array = array()){ 
        if(empty($array)) $array = $_POST; 
        if (is_array($this->_fileds)) { 
            foreach ($this->_fileds as $filed){ 
                list($key,$validate,$message,$breakOnFailure) = $filed; 
                 
                if(empty($key)){ 
                    if(!$validate){ 
                        $this->_message[][] = $message; 
                        if($breakOnFailure) break;  
                    } 
                    continue; 
                } 
                 
                if(!empty($message)) $validate->setMessage($message); 
                if( !$validate->isValid($array[$key]) ){ 
                    $this->_message[$key][] = $validate->getMessages(); 
                    if($breakOnFailure) break;  
                } 
            } 
            if(!empty($this->_message))return false; 
            return true; 
        } 
        return true; 
    } 
     
    public function getMessage(){ 
        return $this->_message; 
    } 
    public function getMessageText(){ 
        $str = ''; 
        foreach ($this->_message as $ms){ 
            foreach ($ms as $m) $str .= $m[0]."\n"; 
        } 
        return $str; 
    } 

?> 


In addition, you can directly extend some verification rule classes. I will talk about it in detail in the next article.