1

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>
3
  • 1
    what is your question?Did you see it?codeigniter.com/user_guide/libraries/… Commented May 25, 2015 at 15:11
  • what i'm saying is that it seems that the form validation class is not passing the information to the view. I can get the else part of the if statement to echo the error in the view, get form_error() to echo in line with the input. Commented May 26, 2015 at 10:57
  • 1
    I got the answer. I for got to call the view again in the else part of the if statement. Commented May 29, 2015 at 18:01

1 Answer 1

1

in the controller do the following:

<?php
class Dwg_issue extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library('session');
        $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
        {
            $data['form_errors'] = $this->_get_validation_errors();

            $this->session->set_flashdata($data);
            redirect('path/of/your/view');
        }
    }

    /**
     * @return array The errors generated during the validation.
     */
    private function _get_validation_errors()
    {
        return array(
            'clientName' => form_error('clientName', NULL, NULL), 
            'clientSurname' => form_error('clientSurname', NULL, NULL), 
            'clientEmail' => form_error('clientEmail', NULL, NULL), 
            'clientCom' => form_error('clientCom', NULL, NULL), 
            'clientPhone' => form_error('clientPhone', NULL, NULL), 
            'clientOfficeNo' => form_error('clientOfficeNo', NULL, NULL), 
            'clientAddress' => form_error('clientAddress', NULL, NULL), 
            'clientPostel' => form_error('clientPostel', NULL, NULL), 
            'clientVat' => form_error('clientVat', NULL, NULL), 
        );
    }
}

In the view modify so that it stays this way:

<div id="body">
    <p>Client information.</p>

<?php 

$form_errors = $this->session->flashdata('form_errors');

echo form_open('dwg_issue/client_add');

echo validation_errors();

echo "<p><lable>Name:</lable>";
echo form_input('clientName',$this->input->post('clientName'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientName'] . "</div>";
}
echo "</p>";

echo "<p><lable>Last name:</lable>";
echo form_input('clientSurname',$this->input->post('clientSurname'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientSurname'] . "</div>";
}
echo "</p>";

echo "<p><lable>Email address:</lable>";
echo form_input('clientEmail',$this->input->post('clientEmail'));

if (count($form_errors)) {
    echo "<div>" . $form_errors['clientEmail'] . "</div>";
}
echo "</p>";

echo "<p><lable>Company Name:</lable>";
echo form_input('clientCom',$this->input->post('clientCom'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientCom'] . "</div>";
}
echo "</p>";

echo "<p><lable>Mobile number:</lable>";
echo form_input('clientPhone',$this->input->post('clientPhone'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientPhone'] . "</div>";
}
echo "</p>";

echo "<p><lable>Office number:</lable>";
echo form_input('clientOfficeNo',$this->input->post('clientOfficeNo'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientOfficeNo'] . "</div>";
}
echo "</p>";

echo "<p><lable>Office address:</lable>";
echo form_textarea('clientAddress',$this->input->post('clientAddress'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientAddress'] . "</div>";
}
echo "</p>";

echo "<p><lable>Postel address:</lable>";
echo form_textarea('clientPostel',$this->input->post('clientPostel'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientPostel'] . "</div>";
}
echo "</p>";

echo "<p><lable>Vat numbrer:</lable>";
echo form_input('clientVat',$this->input->post('clientVat'));
if (count($form_errors)) {
    echo "<div>" . $form_errors['clientVat'] . "</div>";
}
echo "</p>";

echo "<p>";
echo form_submit('edit_submit', 'Add');
echo "</p>";

echo form_close();

?> 

I hope I've helped :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.