Chris
Chris

Reputation: 81

CakePHP Custom Validation Method Not Called With allowEmpty

I have a model that has a first name, last name, and organization/company name field. The user must enter either a first name and a last name OR an organization name.

The issue is that my custom validation method ("validateNames") is never called. For debugging purposes, I have a "die" statement there, rather than real validation logic -- but the die statement is never reached.

My model looks like:

class Contact extends AppModel {
    public $validate = array(
        'first_name' => array(
            'rule' => 'validateNames',
            'allowEmpty' => true,
            'required' => false
        ),
        'last_name' => array(
            'rule' => 'validateNames',
            'allowEmpty' => true,
            'required' => false
        ),
        'organization' => array(
            'rule' => 'validateNames',
            'allowEmpty' => true,
            'required' => false
        )
    );

    public function validateNames($check) {
        die('here');
    }
}

The problem is that as long as I have 'allowEmpty' in the validation rules, my custom validation method is never called (and the 'die' statement is never reached). But if I remove 'allowEmpty', then an HTML "required" attribute is added to each INPUT field (even though I have 'required' => false) -- this prevents the form from being submitted unless all three fields are filled in, when only one (organization) or two (first and last names) are actually required.

Upvotes: 1

Views: 1497

Answers (2)

iqqmuT
iqqmuT

Reputation: 843

Remove allowEmpty option from the validation rules and disable required option when outputting the field in your view. Try this:

Model:

class Contact extends AppModel {
    public $validate = array(
        'first_name' => array(
            'rule' => 'validateNames'
        ),
        'last_name' => array(
            'rule' => 'validateNames'
        ),
        'organization' => array(
            'rule' => 'validateNames'
        )
    );

    public function validateNames($check) {
        die('here');
    }
}

View:

echo $this->Form->input('first_name', array('required' => false));
echo $this->Form->input('last_name', array('required' => false));
echo $this->Form->input('organization', array('required' => false));

Upvotes: 1

liyakat
liyakat

Reputation: 11853

You must have to pass in array if you want to call 2 or more validation with same fields

like

class Contact extends AppModel {
    public $validate = array(
        'first_name' => array(
           'rule1' => array(
                    'rule' => 'validateNames',
                    'message' => 'Must be a valid first name',
                    'allowEmpty' => true
                ),
        ),
        'last_name' => array(
            'rule1' => array(
                    'rule' => 'validateNames',
                    'message' => 'Must be a valid names',
                    'allowEmpty' => true
                ),
        'organization' => array(
            'rule' => 'validateNames',
            'allowEmpty' => true,
            'required' => false
        )
    );
public function validateNames($check) {
    die('here');
}

}

let me know if i can help you more.

Upvotes: 1

Related Questions