Reputation: 1181
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
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
Reputation: 96
In my case it happened because in the controller:
$this->Model->save()
which returned false (first
validation) $this->Model->invalidFields()
which validates again the fields
(second time) and returned the messageTo fix it I changed $this->Model->invalidFields() to $this->Model->validationErrors
to get the error message
Upvotes: 4
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