1

I've declared my validation fields of my view in this way:

public $validate = array(
    'myField' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => true,
            'message' => 'username required'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'required' => 'create',
            'message' => 'Username already used'
        )
    )
);

Is there a way to know (inside the relative controller class) when this message is fired? Because if one of these rules is violated, I would like to perform some tasks, and not simply show message to the user.

1

1 Answer 1

1

Yes, first set the data to the model in your controller:

$this->ModelName->set($this->request->data);

Then, to check if the data validates, use the validates method of the model, which will return true if it validates and false if it doesn’t:

if ($this->ModelName->validates()) {
    // it validated logic
} else {
   // didn't validate logic
   $errors = $this->ModelName->validationErrors;
}

To know more about validationErrors, go to cakephp Validating Data from the Controller article.

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

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.