Styling Codeigniter form error messages

As I get more and more deeply involved in using Codeigniter for my projects I discover bits and pieces that I think might be useful to others.

Today is a very quick tip about styling the error messages that are thrown on a form. Looking at the statement that you have in your view it isn’t immediately obvious how you apply any styling to this at all.

echo form_error('file');

The answer, strangely, is to apply the look and feel through a call to set_error_delimiters in your controller and not in the view. The function takes two parameters: the html code that should be inserted before and after the error text. For example, you might do the following to display the error message text in red:

$this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');

I’m sure that you can think of more inventive ways of styling your error messages but this will give you a starting point. You can also ready more about the function in the Codeigniter guide here.

Leave a Reply

Your email address will not be published. Required fields are marked *