zoukankan      html  css  js  c++  java
  • Yii自定义验证规则

    简单的方法:在 model 内部定义规则

    最简单的定义验证规则的方法是在使用它的模型(model)内部定义。

    比方说,你要检查用户的密码是否足够安全.

    通常情况下你会使用 CRegularExpression 方法验证,但为了本指南,我们假设不存在此验证方法.

    首先在模型(model)中添加两个常量

    const WEAK = 0;
    const STRONG = 1;

    然后在模型(model)的 rules 方法中设置:

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
           array('password', 'passwordStrength', 'strength'=>self::STRONG),
        );
    }

    确保你写的规则不是一个已经存在的规则,否则将会报错.

    现在要做的是在模型(model)中创建一个名称为上面填写的规则的方法(即 passwordStrength)。

    /**
     * check if the user password is strong enough
     * check the password against the pattern requested
     * by the strength parameter
     * This is the 'passwordStrength' validator as declared in rules().
     */
    public function passwordStrength($attribute,$params)
    {
        if ($params['strength'] === self::WEAK)
            $pattern = '/^(?=.*[a-zA-Z0-9]).{5,}$/';  
        elseif ($params['strength'] === self::STRONG)
            $pattern = '/^(?=.*d(?=.*d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/';  
           
        if(!preg_match($pattern, $this->$attribute))
          $this->addError($attribute, 'your password is not strong enough!');
    }

    刚才创建的方法需要两个参数:* $attribute 需要验证的属性* $params 在规则中自定义的参数

    在模型的 rules 方法中我们验证的是 password 属性,所以在验证规则中需要验证的属性值应该是 password.

    在 rules 方法中我们还设置了自定义的参数 strength,它的值将会放到 $params 数组中.

    你会发现在方法中我们使用了 CModel::addError().

    添加错误接受两个参数:第一个参数是在表单中显示错误的属性名,第二个参数时显示的错误信息 。

    完整的方法:继承 CValidator 类

    如果你想把规则使用在多个模型(model)中,最好的方法时继承 CValidator 类。

    继承这个类你可以使用像 CActiveForm::$enableClientValidation (Yii 1.1.7 版本后可用) 类似的其他功能。

    创建类文件

    首先要做的是创建类文件.最好的方法时类的文件名和类名相同,可以使用 yii 的延迟加载(lazy loading)功能。

    让我们在应用(application)的扩展(extensiions)目录(在 protected 文件夹下)下新建一个文件夹.

    将目录命名为: MyValidators

    然后创建文件: passwordStrength.php

    在文件中创建我们的验证方法

    class passwordStrength extends CValidator
    {
           
        public $strength;
           
        private $weak_pattern = '/^(?=.*[a-zA-Z0-9]).{5,}$/';
        private $strong_pattern = '/^(?=.*d(?=.*d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/';
    ...
    }

    在类中创建属性,此属性为在验证规则中使用的参数.

    CValidator 会自动根据参数来填充这些属性.

    我们也创建了两个其他的属性,它们为 preg_match 函数使用的正则表达式.

    现在我们应该重写父类的抽象方法(abstract method) validateAttribute

    /**
     * Validates the attribute of the object.
     * If there is any error, the error message is added to the object.
     * @param CModel $object the object being validated
     * @param string $attribute the attribute being validated
     */
    protected function validateAttribute($object,$attribute)
    {
        // check the strength parameter used in the validation rule of our model
        if ($this->strength == 'weak')
          $pattern = $this->weak_pattern;
        elseif ($this->strength == 'strong')
          $pattern = $this->strong_pattern;
           
        // extract the attribute value from it's model object
        $value=$object->$attribute;
        if(!preg_match($pattern, $value))
        {
            $this->addError($object,$attribute,'your password is too weak!');
        }
    }

    上面的方法我认为就不用解释了.当然你也可以在 if 的条件中使用常量,我推荐使用.

    实现客户端验证

    如果要实现客户端验证还需要重写类中的方法 clientValidateAttribute.

    /**
     * Returns the JavaScript needed for performing client-side validation.
     * @param CModel $object the data object being validated
     * @param string $attribute the name of the attribute to be validated.
     * @return string the client-side validation script.
     * @see CActiveForm::enableClientValidation
     */
    public function clientValidateAttribute($object,$attribute)
    {
           
        // check the strength parameter used in the validation rule of our model
        if ($this->strength == 'weak')
          $pattern = $this->weak_pattern;
        elseif ($this->strength == 'strong')
          $pattern = $this->strong_pattern;     
           
        $condition="!value.match({$pattern})";
           
        return "
    if(".$condition.") {
        messages.push(".CJSON::encode('your password is too weak, you fool!').");
    }
    ";
    }

    正如你看到的此方法简单的返回了一个在验证中将使用到的 javascript.

    最后一步:在模块(model)中怎么使用自定义的验证类

    下面有几种方法来实现:

    你可以在返回 规则数组(ruels array)前 使用 Yii::import 方法,或使用Yii的符号方式:

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
           array('password', 'ext.MyValidators.passwordStrength', 'strength'=>self::STRONG),
        );
    }

    参考:
    HDR
  • 相关阅读:
    Springboot 中AOP的使用
    ElasticSearch 聚合查询百分比
    ElasticSearch 入门
    elasticsearch 关联查询
    spring data elasticsearch多索引查询
    elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词
    es同步mysql同步-logstash
    jpa Specification复杂查询
    java Spring boot使用spring反射
    BeautifulSoup学习记录
  • 原文地址:https://www.cnblogs.com/DaBing0806/p/4744118.html
Copyright © 2011-2022 走看看