PHP extension zf--validate extension _php tutorial

Source: Internet
Author: User
Previously wrote an article about how to extend ZF under the ZF0.6 version. This should be said to be a similar article, but the environment is replaced by the ZF1.0RC1 version.

Before starting the ZF extension, it is recommended to look at some of the command specifications in the ZF Manual (ZF recommended) and hope that the reader will have a better understanding of ZF. If not, you can go to the Phpchian ZF version to learn more, or to Phpeye to find relevant information.

ZF's validator provides powerful verification capabilities, but it is cumbersome to operate in real-world operations. For example, verify that the message is using ZF code as follows


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";
}
}
?>

There is no discovery, or very similar we do not use the ZF verification method. But ZF helped us encapsulate the details of the email verification. So how do we simplify this effect? (The following is how I expanded the call)

$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 (3,15), ' username length must be between 3 and 15! \ '%value%\ ' does not meet the conditions ');
$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 '), ' two input password mismatch ');
$authcode = new Phpbean_img_code ();
$validate, add (' Yanxue8_authcode ', New Phpbean_validate_isequal ($authcode->authcode ($_post[' yanxue8_ Authcode_mdcode '], ' DECODE '), ' Verification code mismatch! ');
if (! $validate-Validator ($_post)) {
Error_page (' Registration failed ', $validate->getmessagetext ());
}
?>

In this way, the code is clear, on the other hand, it is advantageous to deal with the error. So how do we do that?
The key is to phpbean_validate this class.
In fact, the implementation is very simple, the Phpbean_validate::add () method is to add a strip of validation rules in. Then call Phpbean_validate::validator () to verify that it is OK.
The specific implementation steps are as follows:
First, add a Phpbean folder to the Zend's sibling directory and add a validator.php file inside it.
Then, add the definition of phpbean_validate to this class in the validator.php file. Note (You can modify your own file name and path name, but be sure to keep the name of the class consistent).
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 extend some validation rule classes directly. I'll talk about it in the next chapter.

http://www.bkjia.com/PHPjc/318520.html www.bkjia.com true http://www.bkjia.com/PHPjc/318520.html techarticle previously wrote an article about how to extend ZF under the ZF0.6 version. This should be said to be a similar article, but the environment is replaced by the ZF1.0RC1 version. Before you start the ZF extension, it is recommended to take a look at Z ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.