user254153
user254153

Reputation: 1883

Form validation error not showing in view - Codeigniter

I can't figure out the problem whats wrong. Form validation error are showing in controller when var_dump in controller but cannot see in view.

Controller

  function submitform() {

    $pid = 16;
    $data['query'] = $this->viewmodel->get_page($pid);
    $data['notices'] = $this->viewmodel->get_notices();
    $data['events'] = $this->viewmodel->get_events();
    $data['gadgets'] = $this->viewmodel->get_gadgets();

    $this->load->library(array('form_validation', 'session'));
    $this->form_validation->set_rules('fname', 'Full Name', 'required|xss_clean|max_length[100]');
    $this->form_validation->set_rules('p_address', 'Permanet Address', 'required|xss_clean|max_length[200]');
    $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha');

    if (($this->form_validation->run() == FALSE)) {
        var_dump(validation_errors());     //Validation error is show here but not in view.
        die;
        $this->load->view('header');
        $this->load->view('register', $data);
        $this->load->view('eventsfull', $data);
        $this->load->view('sitemapcontent', $data);
        $this->load->view('footer', $data);
    } else {

        $name = $this->input->post('fname');

        $p_address = $this->input->post('p_address');

        $this->viewmodel->add_alumni($fname, $p_address);
        $this->session->set_flashdata('message', 'Your request has been submited for verify.');
        $data['error'] = "Your data submitted sucessfully.";
    }
    redirect('sres/register');
}

view (register.php)

 <?php echo validation_errors(); ?>

 //FORM element

Upvotes: 1

Views: 1472

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Form validation library will work only when your view loaded as $this->load->view(). In redirection it will not work. You may use session flash data to pass errors in redirection view.

$this->session->set_flashdata('errors', validation_errors());

now view file

 <?php var_dump($this->session->flashdata('errors')); ?>

Upvotes: 3

Related Questions