- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
- (06/12) View Layer - Getting count of Entities in View in Cakephp 3TAGS:CakephpCake3
- (05/23) Changing URL in beforeRedirect() in Component - Cakephp 3TAGS:CakephpCake3Web Programming
- (01/28) Foundation CSS Framework ResourcesTAGS:FoundationCss
- (01/27) Change View File from Controller in CakePHP 3TAGS:CakephpCake3UsageWeb Programming
- (01/13) Loading Model/Table Anywhere in Cake3TAGS:Cake3Cakephp
- (10/06) CURL PHP 5.5 Issue on Windows (Using Twilio SDK)
Subscribe to my feed
CakePHP Error Messages Not Showing on Form
Posted on 04/03/2011 at 12:40 pm by Kevin Wentworth
Viewed 14,712 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.