title: Codeigniter form_validation form_error()
body: <p>I have created a form to add a client to my db and I have used the form validation class to validate the form and the validation_errors() to print the form errors.</p>
  
  <p>But now I would like to change this so that the errors display next to the fields. I've tried to use the the form_error(), but unless I echo it in the if statement checking it the form validation has run it doesn't display the error message. </p>
  
  <p>My control</p>
  
  <pre><code>class Dwg_issue extends CI_Controller {
  
  public function __construct(){
      parent::__construct();
      $this-&gt;load-&gt;helper('url');
      $this-&gt;load-&gt;library('form_validation');
      $this-&gt;load-&gt;helper('form');
      $this-&gt;load-&gt;model('model_issue');
  }
  
  public function client_add()
  {
  
      $data['main_content'] = 'client_add';
      $this-&gt;load-&gt;view('includes/template.php', $data);
  
      $this-&gt;form_validation-&gt;set_rules('clientName', 'Name', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientSurname', 'Last name', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientEmail', 'Email address', 'required|trim|valid_email|is_unique[client.clientEmail]');
      $this-&gt;form_validation-&gt;set_rules('clientCom', 'Company Name', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientPhone', 'Mobile number', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientOfficeNo', 'Office number', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientAddress', 'Office address', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientPostel', 'Postel address', 'required|trim');
      $this-&gt;form_validation-&gt;set_rules('clientVat', 'Vat numbrer', 'required|trim|numeric');
  
      $this-&gt;form_validation-&gt;set_message('is_unique', 'This email address has already been registered. Please try again.');
  
      if ($this-&gt;form_validation-&gt;run())
      {
  
          $this-&gt;model_issue-&gt;client_add();   
          redirect ('dwg_issue/client_info');
      }
      else
      {
          echo validation_errors();
  
  
  
      }
  }
  </code></pre>
  
  <p>My view</p>
  
  <pre><code>&lt;h1&gt;Add client details&lt;/h1&gt;
  
      &lt;div id="body"&gt;
          &lt;p&gt;Client information.&lt;/p&gt;
  
      &lt;?php 
  
      echo form_open('dwg_issue/client_add');
  
      echo validation_errors();
  
      echo "&lt;p&gt;&lt;lable&gt;Name:&lt;/lable&gt;";
      echo form_input('clientName',$this-&gt;input-&gt;post('clientName'));
      echo form_error('clientName');
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Last name:&lt;/lable&gt;";
      echo form_input('clientSurname',$this-&gt;input-&gt;post('clientSurname'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Email address:&lt;/lable&gt;";
      echo form_input('clientEmail',$this-&gt;input-&gt;post('clientEmail'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Company Name:&lt;/lable&gt;";
      echo form_input('clientCom',$this-&gt;input-&gt;post('clientCom'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Mobile number:&lt;/lable&gt;";
      echo form_input('clientPhone',$this-&gt;input-&gt;post('clientPhone'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Office number:&lt;/lable&gt;";
      echo form_input('clientOfficeNo',$this-&gt;input-&gt;post('clientOfficeNo'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Office address:&lt;/lable&gt;";
      echo form_textarea('clientAddress',$this-&gt;input-&gt;post('clientAddress'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Postel address:&lt;/lable&gt;";
      echo form_textarea('clientPostel',$this-&gt;input-&gt;post('clientPostel'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;&lt;lable&gt;Vat numbrer:&lt;/lable&gt;";
      echo form_input('clientVat',$this-&gt;input-&gt;post('clientVat'));
      echo "&lt;/p&gt;";
  
      echo "&lt;p&gt;";
      echo form_submit('edit_submit', 'Add');
      echo "&lt;/p&gt;";
  
      echo form_close();
  
      ?&gt; 
  
     &lt;a href="&lt;?php echo base_url() . "index.php/main/logout"; ?&gt;"&gt;Logout&lt;/a&gt;
     &lt;a href="&lt;?php echo base_url() . "index.php/main/members"; ?&gt;"&gt;Members Page&lt;/a&gt;
          &lt;?php 
          echo anchor(base_url(). 'index.php/dwg_issue/client_info','Client list');
      ?&gt;
         &lt;?php 
          if ($this-&gt;session-&gt;userdata('userlevel') == 1)
          {
              echo anchor(base_url().'index.php/user_admin/user_main','User maintenance');
          }
          else echo "User maintenance";
          ?&gt;
      &lt;/div&gt;
  </code></pre>