CakePHP Form Validation: Making Your Error Messages Change Color

When you build a form with Cake and have any sort of validation, it would be nice to have a means for the displayed error message to distinguish itself from the rest of the text (in my particular case, making the text red). Unfortunately, Cake doesn’t seem to have anything built in to easily do this. After playing around with my Cake site for a bit, and doing some extensive Google searching for a simple solution (and coming up lacking), I realized something basic that would do the trick: setting the font color style for <form> (or any enclosing element around <form>) to be the color of my error message, and setting the color for the elements within the form to be the regular text color.

This is working well enough for me for the css:

color: #ff0000;
color: #000;

This only works for displaying error messages though (or rather, for displaying a single message color). If you need a second color, you can probably set a session in your controller to represent a css style, which your view can use to determine which style should get used. But then again, probably the only reason you’d want another color is for success messages, and those are mostly useful if you’re using Ajax, in which case you’ve got better means of message styling then the one I’ve posted here anyhow.

Leave a Reply