duanma8207 2013-07-31 11:37
浏览 30

验证器调用之间的动态规则验证持久性

I've came across this problem a few times, but could never find a solution, only work arounds.

Lets say I have multiple 10 jobs, but depending on the answers given the validation rules are different.

foreach($jobs as $job){

    if(!$this->Job->validates()){
        echo 'Oh no, it didn't validate';
    }

}

The main problem I'm finding is if I set a rule that was triggered only by the first job.

if($this->data['Job']['type'] == 'special'){
    $this->validator()->add('special_field', 'notEmpty', array(
        'rule' => array('notEmpty'),
        'message' => 'Please provide data'
    ));
}

It would also apply to the other 9. So the rules are persistent between calls. So can could remove the rule if it exists.

if($this->data['Job']['type'] == 'special'){
    $this->validator()->add('special_field', 'notEmpty', array(
        'rule' => array('notEmpty'),
        'message' => 'Please provide data'
    ));
}else{
    $this->validator()->remove('special_field', 'notEmpty');
}

But if the rule doesn't exist when it tries to remove it a Fatal Error is thrown.

Is there any way to check a rule exists before I remove it or a way to clear dynamic rules at the start of beforeValidate?

  • 写回答

2条回答 默认 最新

  • duandanai6470 2013-07-31 13:05
    关注

    Interesting problem.

    I have not tested this, but it seems that one can use:

    $this->validator()->remove('special_field');
    

    to remove all the rules for a field.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)