Grouping error messages

Published on and tagged with cakephp  helper

Sometimes you want to display form errors as a group at the top of a page. You could accomplish it by using HtmlHelper::tagErrorMsg(), but well, such a solution is not very elegant imho. So I wrote a little helper which generates the following HTML code to display errors:

<div class="error-messages">
    Please fix the following errors:
        <li>First error message</li>
        <li>Second error message</li>

The usage of the helper is simple. Add it to the helpers array of your controller:

var $helpers = array('Error');

and use it in your view in the following way:

echo $error->showMessages(array('Model' => array('Field1' => 'First error message', 'Field2' => 'Second error message')));

I hope it will be useful for some of you.

Merry Christmas to those who celebrate Christmas!

3 comments baked

Bake a comment

(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License