无为清净楼资源网 Design By www.qnjia.com

YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规则后才能被提交,可以很有效地保证表单安全和信息的有效性。还是给大家具体说明一下:

以下是视图(views)部分的简单代码:

<"row"> 
    <"row"> 
    <"普通TAG",2=>"系统默认TAG"),array('separator'=>'','labelOptions'=>array('class'=>'tagtypelabel'))); "row buttons"> 
    <"htmlcode">
public function rules() 
{ 
  return array( 
    array('tagname,tagtype', 'required'), 
    array('tagtype', 'numerical', 'integerOnly'=>true), 
    array('tagname', 'length', 'max'=>32), 
    array('tagname', 'match', 'pattern'=>'/^[\x{4e00}-\x{9fa5}A-Za-z0-9]+$/u', 
        'message'=>'标签不合法,必须为汉字、字母或者数字!'), 
    array('tagname', 'checktagname', 'on'=>'create,update'),//插入TAG时检查是否已经存在该tag 
    array('tagid, tagname, tagtype', 'safe', 'on'=>'search'), 
  ); 
} 

系统默认有这些验证规则:

boolean : CBooleanValidator 的别名, 确保属性的值是CBooleanValidator::trueValue 或 CBooleanValidator::falseValue . 
captcha : CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码. 
compare : CCompareValidator 的别名, 确保了特性的值等于另一个特性或常量. 
email : CEmailValidator 的别名,确保了特性的值是一个有效的电邮地址. 
default : CDefaultValueValidator 的别名, 为特性指派了一个默认值. 
exist : CExistValidator 的别名, 确保属性值存在于指定的数据表字段中. 
file : CFileValidator 的别名, 确保了特性包含了一个上传文件的名称. 
filter : CFilterValidator 的别名, 使用一个filter转换属性. 
in : CRangeValidator 的别名, 确保了特性出现在一个预订的值列表里. 
length : CStringValidator 的别名, 确保了特性的长度在指定的范围内. 
match : CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式. 
numerical : CNumberValidator 的别名, 确保了特性是一个有效的数字. 
required : CRequiredValidator 的别名, 确保了特性不为空. 
type : CTypeValidator 的别名, 确保了特性为指定的数据类型. 
unique : CUniqueValidator 的别名, 确保了特性在数据表字段中是唯一的. 
url : CUrlValidator 的别名, 确保了特性是一个有效的路径.

基本上还是比较全面的,一般的都够用了,但是还是有时候有的验证需要自定义。就以上面的代码为例,我们在添加TAG时需要检查系统之前是否已经存在这个TAG,如果存在则不让用户添加。这个就需要在添加之前去查询数据库,看该TAG是否已经存在,这里我们就需要自定一个验证规则了。

关键有一下两个步骤:

1、在rules中 添加代码:array('tagname', 'checktagname', 'on'=>'create,update'),//插入TAG时检查是否已经存在该tag

注:我在其中用了 'on'=>'create,update',所以这个验证规则之对create,update场景生效

2、在该模型(models)中添加验证函数:

public function checktagname($attribute,$params){ 
  $oldtag = Tag::model()->findByAttributes(array('tagname'=>$this->tagname)); 
  if($oldtag->tagid > 0){ 
    $this->addError($attribute, '该TAG已经存在!'); 
  } 
} 

其中需要说明的是:

(1)该验证函数的参数必须是($attribute,$params),不能缺少其中任何一个;

(2)$this->addError($attribute, '该TAG已经存在!');这个是你想要在视图中输出的错误提示信息。

就是这么简单,有了这个方法,表单验证的各种想要的规则就都可以自定义了。

下面给大家介绍Yii自定义验证规则

最简单的定义验证规则的方法是在使用它的模型(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 = '/^("htmlcode">
class passwordStrength extends CValidator
{
  public $strength;
  private $weak_pattern = '/^("htmlcode">
/**
 * 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 的条件中使用常量,我推荐使用.

标签:
yii_rules自定义,yii自定义验证

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?