gerl
gerl

Reputation: 1181

cakephp errors - validation displayed twice

I'm using cake 2.3.8 version. I have a registration form where users can enter in a username and password in the form. My model validation looks like:

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A username is required'
        ),
        'alphanumeric' => array(
            'rule'    => 'alphaNumeric',
            'message' => 'Usernames must only contain letters and numbers.'
        )
    ),


    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        )
    ) );

Now the weird thing is in my site when I enter in a username with space in it, the validation is displayed twice. But when I use the Family registration form and enter a username with space in it, the validation error only displays once. Does anyone know what could be the issue?

Upvotes: 2

Views: 1097

Answers (3)

Stallion
Stallion

Reputation: 77

I also got this issue once. But, in my case I found out that, though I had written validate code in the Model, I validated it again in the controller because of which it was showing double validation errors. If you have done the same, then remove

$this->Model->validates()

from controller.

Upvotes: 0

gumaterror
gumaterror

Reputation: 96

In my case it happened because in the controller:

  1. I did a $this->Model->save() which returned false (first validation)
  2. Then, to show validation error I did $this->Model->invalidFields() which validates again the fields (second time) and returned the message

To fix it I changed $this->Model->invalidFields() to $this->Model->validationErrors to get the error message

Upvotes: 4

ndm
ndm

Reputation: 60463

Generally this is because validation is triggered twice. What exactly causes it to be triggered twice is pretty hard to tell without seeing more code (especially involved controllers, components, behaviours and models).

Check whether you are maybe calling Model::validates() manually, additionally to validation that is triggered by the models save operation, or maybe you are even calling it twice manually.

It could also be triggered by a third party component or a behaviour or whatever... you'll need to do some debugging.

Upvotes: 4

Related Questions