CakePHP 中的模型验证检查是否至少设置了其中之一

发布于 2024-11-10 15:24:22 字数 60 浏览 0 评论 0原文

有没有办法验证数据(使用 CakePHP 的模型验证)以确保至少“a”或“b”有数据(不必两者都有数据)。

Is there a way to validate data (using CakePHP's model validation) to make sure that at least "a" or "b" has data (does not have to have data in both).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪#恶を代价 2024-11-17 15:24:22

在你的模型中,做这样的事情。当您执行保存操作时将调用该函数。

已编辑

public $validate = array(
    'a' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    ),
    'b' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    )
);

//Function must be public for Validator to work
//Checks to see if either a or b properties are set and not empty
public function abCheck(){
    if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
         return true;
    }
    return false;
}

In your model, do something like this. The function will be called when you perform a save operation.

EDITED

public $validate = array(
    'a' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    ),
    'b' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    )
);

//Function must be public for Validator to work
//Checks to see if either a or b properties are set and not empty
public function abCheck(){
    if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
         return true;
    }
    return false;
}
请持续率性 2024-11-17 15:24:22

您可以通过“自定义验证”来验证这些条件。

请参阅:添加您自己的验证

you can validate those conditions through "custom validations".

see this: adding your own validation

江南月 2024-11-17 15:24:22

试试这个:

public $validate = array(
     'a' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     ),
     'b' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     )
 );

 //Function must be public for Validator to work
 //Checks to see if either a or b properties are set and not empty
 public function abCheck(){
     if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) > || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
          return 1;
     }
     return -1;
 }

Try this instead:

public $validate = array(
     'a' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     ),
     'b' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     )
 );

 //Function must be public for Validator to work
 //Checks to see if either a or b properties are set and not empty
 public function abCheck(){
     if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) > || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
          return 1;
     }
     return -1;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文