Usage analysis of Zend Framework validator Zend_Validate

不言
Release: 2023-03-31 10:32:02
Original
2394 people have browsed it

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 &#39;Zend/Validate/EmailAddress.php&#39;;
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);
Copy after login

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 &#39;..&#39;
    if ((strpos($value, &#39;..&#39;) !== false) or
      (!preg_match(&#39;/^(.+)@([^@]+)$/&#39;, $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[&#39;domain&#39;]) {
      $hostname = $this->_validateHostnamePart();
    }
    $local = $this->_validateLocalPart();
    // If both parts valid, return true
    if ($local && $length) {
      if (($this->_options[&#39;domain&#39;] && $hostname) || !$this->_options[&#39;domain&#39;]) {
        return true;
      }
    }
    return false;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!