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

Yii-数据模型- rules类验证器方法详解

2015-11-12 18:13 771 查看
public function rules()
{
return array(
array('project_id, type_id, status_id, owner_id, requester_id,', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>256),
array('description', 'length', 'max'=>2000),
array('create_time,create_user_id,update_user_id, update_time', 'safe'),
array('id, name, description, project_id, type_id, status_id, owner_id', 'on'=>'search'),
);
}

//required: 必填
array('title,content','required'),

//match: 正则表达式验证
array('birthday', 'match', 'pattern'=>'%^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$%', 'allowEmpty'=>true, 'message'=>'生日必须是年-月-日格式'),

//email:邮箱格式验证
array('user_mail', 'email'),

//url:URL格式验证
array('user', 'url'),

//unique:唯一性验证
array('username', 'unique','caseSensitive'=>false,'className'=>'user','message'=>'用户名"{value}"已经被注册,请更换'),
//caseSensitive 定义大小写是否敏感

//compare:一致性验证
array('repassword', 'compare', 'compareAttribute'=>'password','message'=>'两处输入的密码并不一致'),

//length:长度验证

//in: 验证此属性值在列表之中(通过range指定)。

//numerical: 验证此属性的值是一个数字

//captcha: 验证属性值和验证码中显示的一致
array('verifyCode','captcha'),

//type: 验证属性的类型是否为type所指定的类型.

//file: 验证一个属性是否接收到一个有效的上传文件

//default: 属性指定默认值

//exist: 验证属性值在数据库中是否存在

//boolean: 验证布尔属性值

//date: 检验此属性是否描述了一个日期、时间或日期时间

//safe: 属性标志为在批量赋值时是安全的。

//unsafe: 标志为不安全,所以他们不能被批量赋值。


return array(

//必须填写
array('email, username, password,agree,verifyPassword,verifyCode', 'required'),

//检查用户名是否重复
array('email','unique','message'=>'用户名已占用'),

//用户输入最大的字符限制
array('email, username', 'length', 'max'=>64),

//限制用户最小长度和最大长度
array('username', 'length', 'max'=>7, 'min'=>2, 'tooLong'=>'用户名请输入长度为4-14个字符', 'tooShort'=>'用户名请输入长度为2-7个字'),

//限制密码最小长度和最大长度
array('password', 'length', 'max'=>22, 'min'=>6, 'tooLong'=>'密码请输入长度为6-22位字符', 'tooShort'=>'密码请输入长度为6-22位字符'),

//判断用户输入的是否是邮件
array('email','email','message'=>'邮箱格式错误'),

//检查用户输入的密码是否是一样的
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'请再输入确认密码'),

//检查用户是否同意协议条款
array('agree', 'required', 'requiredValue'=>true,'message'=>'请确认是否同意隐私权协议条款'),

//判断是否是日期格式
array('created', 'date', 'format'=>'yyyy/MM/dd/ HH:mm:ss'),

//判断是否包含输入的字符
array('superuser', 'in', 'range' => array(0, 1)),

//正则验证器:
array('name','match','pattern'=>'/^[a-z0-9\-_]+$/'),

//数字验证器:
array('id', 'numerical', 'min'=>1, 'max'=>10, 'integerOnly'=>true),

//类型验证 integer,float,string,array,date,time,datetime
array('created', 'type', 'datetime'),

//文件验证:
array('filename', 'file', 'allowEmpty'=>true, 'types'=>'zip, rar, xls, pdf, ppt','tooLarge'=>'图片不要超过800K'),

array('url',
'file',    //定义为file类型
'allowEmpty'=>true,
'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx',   //上传文件的类型
'maxSize'=>1024*1024*10,    //上传大小限制,注意不是php.ini中的上传文件大小
'tooLarge'=>'文件大于10M,上传失败!请上传小于10M的文件!'
),
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: