PHP extended Zf--validate Extended _php Tips

Source: Internet
Author: User
Tags error handling zend
Previously wrote an article on how to extend the ZF under the ZF0.6 version. This should be said to be a similar article, but the environment changed to ZF1.0RC1 version.

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

ZF's validator provides a powerful validation capability, but it's too cumbersome to actually operate. For example, to validate a message, use the ZF code 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";
}
}
?>

Did you find it, or is it similar to the way we don't use ZF authentication? But the ZF has helped us to encapsulate the details of the email verification. So how do we simplify this effect? (Here's how I call it after I extend it)

<?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 (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 passwords do not match ');
$authcode = new Phpbean_img_code ();
$validate-> Add (' Yanxue8_authcode ', New Phpbean_validate_isequal ($authcode->authcode ($_post[' yanxue8_ Authcode_mdcode '], ' DECODE '), ' CAPTCHA does not match! ');
if (! $validate-> Validator ($_post)) {
Error_page (' Registration failure ', $validate->getmessagetext ());
}
?>

In this way, the code is clear on the one hand, and on the other hand it is advantageous to agree on error handling. So how do you 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 set of validation rules in. Then call Phpbean_validate::validator () to verify the OK.
The following steps are implemented:
First, add a Phpbean folder to the Zend sibling directory and add a validator.php file inside it.
Then, in the validator.php file, add the definition of the Phpbean_validate class. Note (You can modify your own file name and pathname, but be sure to keep it consistent with the name of the class).
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 say it in detail in the next chapter.

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.