您的位置:首页 > 编程语言 > PHP开发

yii 字段验证的使用

2016-01-28 22:50 761 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: