关于自定义rules验证器的param选项

已经在模型中定义好了一个验证器,使用到了param,但是不知道怎么在rules中加入param参数,求教。

array(‘date’, ‘ValidatorName’, ‘on’=>‘create’, $params) 这样是无效的




/**

 * @param string 所要验证的特性的名字

 * @param array 验证规则中指定的选项

 */

public function ValidatorName($attribute,$params) { ... }



在类里加上public属性。public $zzz;

rules 里array(‘date’, ‘ValidatorName’, ‘on’=>‘create’)

attributeLabels里加’zzz’=>‘NNN’;

Controller里

$model = new Xxx(‘create’);

$model->attributes=$_POST[‘YYY’];

这样。

public function ValidatorName() { $this->zzz;(这样应该就有值了) }

不好意思,没看懂,为什么要在attributeLabels里加字段呢?

rules里面不可以直接赋参数吗?

http://www.yiiframework.com/doc/api/1.1/CModel#rules-detail

如果attributeLabels已经有了你要验证的,你就直接$this->取值。