yii 字段验证的使用

public function rules()
    {

        return array(
            array('name', 'required', 'requiredValue' => "1", 'strict' => true, 'on' => 'required', 'message' => 'This is message.'),
            array('name', 'boolean', 'strict' => true, 'trueValue' => 'a', 'falseValue' => 'b', 'on' => 'bool'),
            array('name', 'compare', 'compareValue' => 'wangyi', 'operator' => '!=', 'on' => 'compare'),
            array('name', 'default', 'value' => 'wangyi', 'on' => 'default'),
            array('name', 'email', 'allowEmpty' => false, 'on' => 'email'),
            array('salon_id', 'exist', 'className' => 'SalonAr', 'on' => 'exist'),
            array('name', 'filter', 'filter' => function ($value) {
                return 'filter_' . $value;
            }, 'on' => 'filter'),
            // array('name', 'inline', 'method' => 'inline', 'params' => 'hello', 'on' => 'inline'),   //没搞懂
            array('name', 'numerical', 'min' => 0, 'max' => 100, 'integerOnly' => true, 'tooSmall' => 'the value is too small', 'tooBig' => 'the value is too big', 'on' => 'number'),
            array('name', 'in', 'range' => array(1, 2, 3, 4, 5), 'on' => 'range'),
            array('name', 'length', 'min' => 10, 'is' => 20, 'max' => 100, 'tooShort' => 'string is too short', 'tooLong' => 'string is too long', 'on' => 'length'),
            array('name', 'match', 'pattern' => '/^wangyi$/', 'not' => false, 'on' => 'match'),
            array('name', 'type', 'type' => 'string', 'on' => 'string'),
            array('name', 'type', 'type' => 'integer', 'on' => 'integer'),
            array('name', 'type', 'type' => 'float', 'on' => 'float'),
            array('name', 'type', 'type' => 'date', 'dateFormat' => 'yyyy-MM-dd', 'on' => 'date'),
            array('name', 'type', 'type' => 'time', 'timeFormat' => 'HH:mm', 'on' => 'time'),
            array('name', 'type', 'type' => 'datetime', 'datetimeFormat' => 'yyyy-MM-dd HH:mm', 'on' => 'datetime'),
            array('name', 'type', 'type' => 'array', 'on' => 'array'),
            array('salon_id', 'unique', 'className' => 'SalonAr', 'on' => 'unique'),
            array('name', 'url', 'on' => 'url'),
            array('name,id', 'checker', 'hello' => 'world', 'on' => 'diy'),
        );
    }
    //自定义    $attribute 为要验证的字段的名   $params  为除了验证字段和验证类型外的 其他所有参数的数组
    public function checker($attribute, $params)
    {
        if ($this->name != 'wangyi') {
            $this->addError($attribute, 'this name must be wangyi');
            return false;
        }
        return true;
    }

 

 

转载于:https://my.oschina.net/chunhei2008/blog/611118

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值