zoukankan      html  css  js  c++  java
  • Zend Framework学习之校验器Zend_Validate

    引言:

    是对输入内容进行检查,并生成一个布尔结果来表明内容是否被成功校验的机制。

    如果isValid()方法返回False,子类的getMessage()方法将返回一个消息数组来解释校验失败的原因。
    为了正确地返回消息与错误内容,对于isValid()方法的每次调用,都需要清除前一个isValid()方法调用所导致的消息和错误。

    案例:

    <?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);

    结果:

    输入的E-mail地址:abc@123.com有效!

    输入的E-mail地址:abc#123.com无效!失败消息为:

    'abc#123.com' is not a valid email address in the basic format local-part@hostname

    emailAddressInvalidFormat

    说明:

    在引入类之后,定义一个验证函数,在函数中实例化类。用isValid()方法来进行验证,不同的子类验证器验证的内容是不一样的。

    同时通过getMessages()方法和getErrors()方法来。

    源码赏析:

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

    解析:

    这是主要的验证函数内容,分成了多种情况进行验证,有是否字符串,有是否符合邮箱规则,有长度是否符合,最终都符合才返回true。

  • 相关阅读:
    Linux驱动之USB(个人)
    iptables命令使用详解
    python操作mysql——mysql.connector
    linux下NFS实战
    CentOS6上ftp服务器搭建实战
    CentOS7下mariadb日常管理
    CentOS7配置httpd虚拟主机
    httpd常见配置
    常见加密算法
    HTTP安全通信:Https和SSL
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/2998430.html
Copyright © 2011-2022 走看看