0

大家好,我正在为我的项目使用 cakephp 框架。在我的表单中,我选中了一个复选框,点击显示了另外两个文本框。通过使用 cakephp validates 方法,我验证了表单数据,但我希望在未选中复选框时避免验证该文本框。它仅在选中复选框时检查。所以请帮助我。

提前致谢

4

3 回答 3

1

您可以在验证服务调用之前使用您的模型,并为此模型添加额外的验证标准。

例如:

function beforeValidate($options = array())
{
    if(!empty($this->data['Model']['fieldOne']))
        $this->validate['fieldTwo'] = array(/*normal validation rules*/);
    return true; // Needed or validation fails
}
于 2010-06-22T18:13:33.783 回答
0

You can use custom validation methods:

var $validate = array(
    'checkbox1' => 'checkboxRule'
);

// If checkbox1 is checked, requires checkbox2 to be checked as well
function checkboxRule() {
    if (!empty($this->data[$this->alias]['checkbox1'])) {
        return !empty($this->data[$this->alias]['checkbox2']);
    }
    return true;
}

You can also invalidate other fields, like checkbox2, at the same time by calling $this->invalidate('checkbox2') in your custom method.

于 2010-06-23T01:44:19.613 回答
0

此外,您可以像这样在控制器中取消验证:

unset($this->Model->validate);
于 2010-07-26T23:34:03.037 回答