Home > Article > Backend Development > Usage analysis of Zend Framework validator Zend_Validate
This article mainly introduces the usage of Zend Framework validator Zend_Validate, and analyzes the functions, usage skills and related precautions of the validator Zend_Validate in the form of examples. Friends in need can refer to the examples of this article
Describes the usage of Zend Framework validator Zend_Validate. Share it with everyone for your reference, the details are as follows:
Introduction:
checks the input content and generates a Boolean result to indicate whether the content has been successfully verified. Mechanisms.
If the isValid() method returns False, the subclass's getMessage() method will return a message array to explain the reason for the verification failure.
In order to return the message and error content correctly, for each call to the isValid() method, the message and error caused by the previous isValid() method call need to be cleared.
Case:
<?php require_once 'Zend/Validate/EmailAddress.php'; function c_email($email) { $validator = new Zend_Validate_EmailAddress(); if($validator->isValid($email)){ echo "输入的E-mail地址:"; echo $email."有效!<p>"; }else{ echo "输入的E-mail地址:"; echo $email."无效!"; echo "失败消息为:<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);
Result:
Entered E-mail address: abc@123.com efficient!
The entered email address: abc#123.com is invalid! The failure message is:
'abc#123.com' is not a valid email address in the basic format local-part@hostname
emailAddressInvalidFormat
Description:
After introducing the class, define a verification function and instantiate the class in the function. Use the isValid() method to perform verification. Different subclass validators verify different contents.
At the same time, use the getMessages() method and the getErrors() method.
Source code 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; }
Analysis:
This is the main verification function content. It is divided into multiple situations for verification, including whether it is a string, whether it conforms to the mailbox rules, and whether the length is consistent. In the end, true will be returned when all are consistent.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Zend
Usage analysis of Zend_View component in Framework
The above is the detailed content of Usage analysis of Zend Framework validator Zend_Validate. For more information, please follow other related articles on the PHP Chinese website!