Displaying errors next to form items

In the preceding example, we displayed errors one by one at the top of the HTML page; however, you may wish to display each individual error closer to the form element to which it refers.

How to do it...

We're going to amend the /path/to/codeigniter/application/views/new_record.php file.

  1. Amend the code to reflect the following (changes in bold):
    <?php echo form_open('form/submit_form') ; ?> <?php if (validation_errors()) : ?> <h3>Whoops! There was an error:</h3> <?php endif; ?> <table border="0" > <tr> <td>First Name</td> <?php if (form_error('first_name')) : ?> <?php echo form_error('first_name') ; ?> <?php endif ; ?> <td><?php echo form_input(array('name' => 'first_name', 'id' => 'first_name', 'value' => set_value('first_name', ...

Get CodeIgniter 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.