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.
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.
My control
class Dwg_issue extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->model('model_issue');
}
public function client_add()
{
$data['main_content'] = 'client_add';
$this->load->view('includes/template.php', $data);
$this->form_validation->set_rules('clientName', 'Name', 'required|trim');
$this->form_validation->set_rules('clientSurname', 'Last name', 'required|trim');
$this->form_validation->set_rules('clientEmail', 'Email address', 'required|trim|valid_email|is_unique[client.clientEmail]');
$this->form_validation->set_rules('clientCom', 'Company Name', 'required|trim');
$this->form_validation->set_rules('clientPhone', 'Mobile number', 'required|trim');
$this->form_validation->set_rules('clientOfficeNo', 'Office number', 'required|trim');
$this->form_validation->set_rules('clientAddress', 'Office address', 'required|trim');
$this->form_validation->set_rules('clientPostel', 'Postel address', 'required|trim');
$this->form_validation->set_rules('clientVat', 'Vat numbrer', 'required|trim|numeric');
$this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.');
if ($this->form_validation->run())
{
$this->model_issue->client_add();
redirect ('dwg_issue/client_info');
}
else
{
echo validation_errors();
}
}
My view
<h1>Add client details</h1>
<div id="body">
<p>Client information.</p>
<?php
echo form_open('dwg_issue/client_add');
echo validation_errors();
echo "<p><lable>Name:</lable>";
echo form_input('clientName',$this->input->post('clientName'));
echo form_error('clientName');
echo "</p>";
echo "<p><lable>Last name:</lable>";
echo form_input('clientSurname',$this->input->post('clientSurname'));
echo "</p>";
echo "<p><lable>Email address:</lable>";
echo form_input('clientEmail',$this->input->post('clientEmail'));
echo "</p>";
echo "<p><lable>Company Name:</lable>";
echo form_input('clientCom',$this->input->post('clientCom'));
echo "</p>";
echo "<p><lable>Mobile number:</lable>";
echo form_input('clientPhone',$this->input->post('clientPhone'));
echo "</p>";
echo "<p><lable>Office number:</lable>";
echo form_input('clientOfficeNo',$this->input->post('clientOfficeNo'));
echo "</p>";
echo "<p><lable>Office address:</lable>";
echo form_textarea('clientAddress',$this->input->post('clientAddress'));
echo "</p>";
echo "<p><lable>Postel address:</lable>";
echo form_textarea('clientPostel',$this->input->post('clientPostel'));
echo "</p>";
echo "<p><lable>Vat numbrer:</lable>";
echo form_input('clientVat',$this->input->post('clientVat'));
echo "</p>";
echo "<p>";
echo form_submit('edit_submit', 'Add');
echo "</p>";
echo form_close();
?>
<a href="<?php echo base_url() . "index.php/main/logout"; ?>">Logout</a>
<a href="<?php echo base_url() . "index.php/main/members"; ?>">Members Page</a>
<?php
echo anchor(base_url(). 'index.php/dwg_issue/client_info','Client list');
?>
<?php
if ($this->session->userdata('userlevel') == 1)
{
echo anchor(base_url().'index.php/user_admin/user_main','User maintenance');
}
else echo "User maintenance";
?>
</div>