Question

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?

Was it helpful?

Solution

Somehow, that CakePHP version seems to have a problem with the notEmpty-rule. I changed my model, now it works:

    var $validate = array(
        'name' => array(
            'rule' => array('minLength', 1),
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Geben Sie Ihren Namen ein!'
        ),
        'address' => array(
            'rule' => array('minLength', 1),
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Geben Sie Straße und Hausnummer ein!'
        ),
        'city' => array(
            'rule' => array('minLength', 1),
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Geben Sie Postleitzahl und Ort ein!'
        ),
        'email' => array(
            'rule' => array('minLength', 1),
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Geben Sie Ihre E-Mail-Adresse ein!'
        )
    );

OTHER TIPS

Maybe You should set $this->data['Cart'] for validation? and You can check Your validation errors like:

debug($this->Cart->validationErrors);

And You want to save data You, can do that with:

// ...
            if($this->Cart->save($this->request->data['Cart'])){
                // ...
            }else{
                debug($this->Cart->validationErrors); die();
                // ...
            }

CakePhp by default set validate => true option for save.

UPDATE You can also:

  1. set table to true and try to save data without validation (then You will know if is You validation error or Your data form error or set table to false error)
  2. check form is form open and form close corretly, check your names inputs too
  3. try to test data with one rule only, or try other rules
  4. check your models naming, try debug($this->model); is it corretly set everything
  5. Try validate data with model name, like:

        $this_data['Cart'] = $this->data['Cart'];
        $this->Cart->set($this_data);
        if ($this->Cart->validates()){
            // ...
        } else{
            debug($this->Cart->invalidFields()); die();
        }
    
  6. Set 'allowEmpty' => false to Your model rules like:

_

 var $validate = array(
         'yourFieldName' => array(
            'rule' =>  'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => '...'
            )
          );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top