MainelyDesign.com Blog

CakePHP Error Messages Not Showing on Form

Posted on 04/03/2011 at 12:40 pm by Kevin Wentworth
Viewed 20,334 times | 2 comments

I ran into an issue today that I've run into before, except I couldn't remember what I did to fix it (hence this article).  I was using CakePHP validation rules to validate a user account details.  The CakePHP validation was properly failing (the form data wasn't being saved) and I could tell the validate array was set correctly because the required fields were having the class .required added to them.  The only problem?  No error messages were being output to the form.  The form validation would fail, but the CakePHP error messages were not being shown.

CakePHP Error Messages Not Showing Up?

The fix is simple (stupidly simple).  Don't overwrite $this->data AFTER your $this->Model->save() call and/or your $this->Model->validate() call(s).  What I was doing was stupid... I was reading the data for the form AFTER trying to save the posted data. 

CakePHP Validation Errors Exist Until You Overwrite $this->data

The moral of the story, when you use $this->Model->read(null, $id) to fetch results, $this->data is overwritten, which was the whole problem.

To fix, wrap your $this->Model->read(null, $id) in an if(empty($this->data)) assertion.

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for CakePHP Error Messages Not Showing on Form

Cakephp | Cakephp 13 | Web Programming | Errors | Usage

Comments for this Posting

Posted by Michael

on 12/4/11

Aaaaaaaaaaaah! thanks! wow, the hours wasted trying to debug this one!

thank you so much :)

Posted by Vishal

on 22/9/11

Thanks man. How did you manage to find this thing?
May be you are super human.
Thanks

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design