开发者

how to prevent an element to validate

hi to all i am using c开发者_StackOverflow中文版akephp framework for my project. in on of my form i take a checkbox on click on this two other text box are shown. by using cakephp validates method i validate the form data but i want that when the checkbox is not checked then it avoid the validation for that text box. it check only when the checkbox is checked. so plz help me.

thanks in advance


You can use your models beforeValidate servicecall for that and add extra validation criteria for this model.

For example:

function beforeValidate($options = array())
{
    if(!empty($this->data['Model']['fieldOne']))
        $this->validate['fieldTwo'] = array(/*normal validation rules*/);
    return true; // Needed or validation fails
}


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.


Also, you can unset the validation in your controller like this:

unset($this->Model->validate);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜