I wrote an article about how to extend ZF in ZF0.6. This is a similar article, but the environment is changed to ZF1.0RC1.
Before starting the ZF extension, we recommend that you first check some command specifications in the ZF Manual (recommended for use by ZF), and hope readers can have a better understanding of ZF. If not, you can go to the phpchian zf version for details or go to phpeye to find relevant information.
ZF's validator provides powerful verification functions, but it is too cumbersome in actual operations. For example, to verify the email, the ZF code is used as follows:
<? 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 ";
}
}
?>
Have you found that it is similar that we do not use the ZF verification method. Only ZF helped us encapsulate the details of email verification. So how can we simplify it to 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 (), 'email address is incorrect! ');
$ Validate-> add ('username', new Zend_Validate_StringLength (), 'username length must be between 3 and 15! \ '% Value % \' does not meet the condition ');
$ 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']), 'mismatch between two passwords ');
$ 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 ('registration failed', $ validate-> getMessageText ());
}
?>
In this way, on the one hand, the code is clear, and on the other hand, it is advantageous to agree to handle errors. So how can this be done?
The key is the Phpbean_Validate class.
In fact, it is easy to implement. The Phpbean_Validate: add () method adds verification rules. Then call Phpbean_Validate: validator () to verify it.
The specific implementation steps are as follows:
First, add a phpbean folder in the zend directory at the same level, and then add a Validator. php file in it.
Then, add the Phpbean_Validate class definition in the validator. php file. Note (you can change it to your own file name and path name, but be sure to keep it consistent with the class name ).
Here, I will provide the implementation process of my Phpbean_Validate class for your 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 validation rule classes. I will go into details in the next article.