views:

37

answers:

3

hi to all i am using cakephp 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

A: 

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
}
kaupov
A: 

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.

deceze
A: 

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

unset($this->Model->validate);
Nick
thanksbut it prevent the validation of all the model element.and i wanna prevent selected
Praveen kalal