1

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.

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.