In my controller, I am trying to validate some data sent by the user, but strangely, the validation fails even if everything seems to be correct:
if(isset($this->data['Cart'])) { //See below for what is in $this->data!!
$this->Cart->set($this->data);
if($this->Cart->validates()) {
This is exactly how you should do it in CakePHP v1.3 (see http://book.cakephp.org/1.3/en/The-Manual/Common-Tasks-With-CakePHP/Data-Validation.html)
The Model:
class Cart extends AppModel
{
var $useTable = false;
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Geben Sie Ihren Namen ein!'
),
'address' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Geben Sie Straße und Hausnummer ein!'
),
'city' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Geben Sie Postleitzahl und Ort ein!'
),
'email' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Geben Sie Ihre E-Mail-Adresse ein!'
)
);
}
Result of var_dump($this->data)
:
array(2) {
["amount"]=>
array(2) {
["san02l"]=>
string(1) "5"
["pp02l"]=>
string(1) "1"
}
["Cart"]=>
array(7) {
["company"]=>
string(13) "SomeCompany"
["name"]=>
string(10) "Lars Ebert"
["address"]=>
string(17) "Street No"
["city"]=>
string(18) "12345 Somewhere"
["phone"]=>
string(10) "0123456789"
["email"]=>
string(22) "mail@example.com"
["comments"]=>
string(26) "Dies ist
nur
Ein Test!!"
}
}
All four error messages are shown every time. I am used to CakePHP v2, so maybe I am overlooking something very obvious? Why will it not validate?
Update
I also tried $this->Cart->set($this->data['Cart']);
and $this->Cart->set(array('Cart' => $this->data['Cart']));
as suggested by skywalker. Still all four errors get displayed.
Also strange: When trying $this->Cart->validates(array('fieldList' => array('name')))
, all four errors get displayed. Shouldn't only one error be shown in this case?
The problem seems to be occurring independently of $this->data
. I just tried setting the name manually:
if(isset($this->data['Cart'])) {
$this->Cart->set($this->data['Cart']);
$this->Cart->set('name', 'Lars Ebert');
if($this->Cart->validates()) {
This still results in all error messages shown. Should not at least the name-error disappear?