Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p><strong>Quick answer</strong>: Before trying to actually save the data, I validated the data separately like (notice 'validate'=>'only'):</p> <pre><code>if($this-&gt;ModelName-&gt;saveAll($this-&gt;data, array('validate' =&gt; 'only'))) { // proceed to save... } </code></pre> <p>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).</p> <hr> <p><strong>Note:</strong> 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'):</p> <pre><code>if($this-&gt;ModelName-&gt;saveAll($this-&gt;data, array('validate' =&gt; 'first'))) </code></pre> <p>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')".</p> <hr> <p><strong>Further explanation</strong>: The issue does seem to be related to using numerically indexed fields in the form. For example:</p> <pre><code>$this-&gt;Form-&gt;input("ModelName.0.field_name"); </code></pre> <p>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 <em>access to</em> the validation error. This can be seen in the view by using (notice no numerical index in these lines):</p> <pre><code>if($this-&gt;Form-&gt;isFieldError("ModelName.field_name")) { echo $this-&gt;Form-&gt;error("ModelName.field_name"); } </code></pre> <p>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).</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload