2

I've looked at loads of forums about validation errors not showing and tried various things but to no avail...

Basically, the validation is correctly recognising the fields do not have values when they should, however the error messages don't 'automagically' appear below the input boxes.

Model validation rule is shown below:

var $validate = array(
    'description' => array(
        'rule' => 'notEmpty',
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter a description of the change'
    )
);

echo pr($this->data); output is shown below:

Array
(
[Change] => Array
    (
        [0] => Array
            (
                [id] => 3237
                [cn_id] => 5132
                [req_id] => 25
                [description] => 
            )

        [1] => Array
            (
                [id] => 3238
                [cn_id] => 5132
                [req_id] => 22
                [description] => 
            )

        [2] => Array
            (
                [id] => 3239
                [cn_id] => 5132
                [req_id] => 4
                [description] => 
            )

    )

)

echo pr($this->Change->invalidFields()); output is shown below:

Array
(
[0] => Array
    (
        [description] => Please enter a description of the change
    )

[1] => Array
    (
        [description] => Please enter a description of the change
    )

[2] => Array
    (
        [description] => Please enter a description of the change
    )

[description] => Please enter a description of the change
)

So, it is generating the errors messages for display, but they don't actually display in the view, and I don't know why?

Excerpt from the 'view' code is show below:

<?php echo $form->input('Change.'.$i.'.description', 
array('value' => $cn['Change'][$i]['description'],
    'label' => $engReq['Req']['description'])); ?>  

Does anybody have ideas why the error messages are not showing?

tereško
  • 58,060
  • 25
  • 98
  • 150
weedave
  • 237
  • 1
  • 7
  • 22
  • Not sure, but the validation issue seems specifically related to using `Model::saveAll()` if that helps.. – deizel. Jun 10 '11 at 16:21

2 Answers2

1

I experienced the same issue with a hasMany model (where the form had numerically indexed fields) and came up with a validation solution that worked for me.

Quick answer: Before trying to actually save the data, I validated the data separately like (notice 'validate'=>'only'):

if($this->ModelName->saveAll($this->data, array('validate' => 'only'))) {
  // proceed to save...
}

Doing it this way gave me the model's validation error message in the form, right under the input field that failed the validation (the normal Cake way of showing the validation error).


Note: I could not use saveAll() to actually save my data (I'll explain why in a minute). If I could use saveAll() to actually save the data, I could have gotten the validation at the same time as I saved by using (notice 'validate' => 'first'):

if($this->ModelName->saveAll($this->data, array('validate' => 'first'))) 

However, I could not use saveAll() to actually save the data, due to the fact that I needed to use a transaction to save several models at once, where some of the models were not directly related to other models. saveAll() will only save the model on which it is called, plus models directly related to it. Since Cake does not currently support nested transactions, and saveAll() uses one transaction automatically, I had to use save() on my models and start and end my transaction manually. However, this caused me to loose the validation message in my form on the hasMany items, even if I saved by using "$this->ModelName->save($this->data, array('validate'=>'first')".


Further explanation: The issue does seem to be related to using numerically indexed fields in the form. For example:

$this->Form->input("ModelName.0.field_name");

It seems this indexing scheme is the proper way to handle hasMany items in the form, but the validation messages would not find their way to this form input. It is interesting to notice that my view did in fact have access to the validation error. This can be seen in the view by using (notice no numerical index in these lines):

if($this->Form->isFieldError("ModelName.field_name")) {
  echo $this->Form->error("ModelName.field_name");
}

Putting these lines after the '$this->Form->input("ModelName.0.field_name")' inserted a the validation message into the page, just not in the same div as the input field (and thus it didn't look ideal).

I couldn't figure out a way to tell Cake to use that validation message in the '$this->Form->input("ModelName.0.field_name")'. So I resorted to the 'validate' => 'only' method described earlier, which is working well for me.

Jason Frank
  • 3,842
  • 31
  • 35
0

shouldnt it be

var $validate = array(
  'description' => array(
    'notEmpty' => array(
        'rule' => 'notEmpty',
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter a description of the change'
    )
  )
);

?

mark
  • 21,691
  • 3
  • 49
  • 71
  • 1
    The OP's array should (and does) work as there is only one validation rule for that field. Your array is the common and correct format to use when a field has multiple rules, though the `'notEmpty'` *key* is entirely optional (and arbitrary). ;) – deizel. Jun 10 '11 at 16:14
  • Tried the array option anyway but it still doesn't show the error messages on the form. :| – weedave Jun 15 '11 at 07:59