Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
CakePHP Error Messages Not Showing on Form
Posted on 04/03/2011 at 12:40 pm by Kevin Wentworth
Viewed 20,369 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
Tags for CakePHP Error Messages Not Showing on Form
Cakephp | Cakephp 13 | Web Programming | Errors | Usage
Comments for this Posting
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!
Posted by Michael
on 12/4/11
Aaaaaaaaaaaah! thanks! wow, the hours wasted trying to debug this one!
thank you so much :)