This article mainly introduces the Zend Framework calibrator zend_validate usage, combined with the example form analysis of the validator zend_validate function, the use of skills and related considerations, the need for friends can refer to the following
Examples of this article describe the use of the Zend Framework Validator zend_validate. Share to everyone for your reference, as follows:
Introduction:
is the mechanism by which the input is checked and a Boolean result is generated to indicate whether the content was successfully validated.
If the IsValid () method returns False, the GetMessage () method of the subclass returns an array of messages explaining why the checksum failed.
To correctly return the message and error content, each call to the IsValid () method needs to clear the message and error caused by the previous IsValid () method call.
Case:
<?phprequire_once ' zend/validate/emailaddress.php '; function C_email ($email) { $validator = new Zend_validate_ EmailAddress (); if ($validator->isvalid ($email)) { echo "input e-mail address:"; echo $email. " Effective! <p> "; } else{ echo "Input e-mail address:"; echo $email. " Invalid! "; echo "Failure message for:<p>"; foreach ($validator->getmessages () as $message) { echo $message. <p> "; } foreach ($validator->geterrors () as $error) { echo $error. <p> ";}} } $e _m1 = "abc@123.com"; $e _m2 = "abc#123.com"; C_email ($e _m1); C_email ($e _m2);
Results:
Enter the e-mail address: abc@123.com effective!
Input e-mail address: abc#123.com Invalid! The failure message is:
' abc#123.com ' isn't a valid email address in the basic format local-part@hostname
Emailaddressinvalidformat
Description
After the class is introduced, define a validation function that instantiates the class in the function. The IsValid () method is used to verify that the content of different subclass validators is not the same.
At the same time through the Getmessages () method and the GetErrors () method.
SOURCE Appreciation:
Public Function IsValid ($value) {if (!is_string ($value)) {$this->_error (self::invalid); return false; } $matches = Array (); $length = true; $this->_setvalue ($value); Split email address up and disallow ' ... ' if (Strpos ($value, '. ')!== false) or (!preg_match ('/^ (. +) @ ([^@]+) $/', $value, $matches))) {$this->_error ( Self::invalid_format); return false; } $this->_localpart = $matches [1]; $this->_hostname = $matches [2]; if ((strlen ($this->_localpart) > 64) | | (Strlen ($this->_hostname) > 255)) {$length = false; $this->_error (self::length_exceeded); }//Match hostname part if ($this->_options[' domain ') {$hostname = $this->_validatehostnamepart (); } $local = $this->_validatelocalpart (); If both parts valid, return True if ($local && $length) {if ($this->_options[' domain ' && $hostname) | | ! $this->_options[' domain ') { return true; }} return false;}
Analytical:
This is the main validation function content, divided into a variety of cases to verify whether there is a string, whether it conforms to the mailbox rules, whether the length of the match, and eventually all conform to return true.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!