Codeigniter form validation is not showing errors

codeigniter, forms, php, validation

Solution

Turns out my model was the culprit. It was extending CI_Controller not CI_Model. Thanks to everyone who took a look at this. This code works.

Problem

I'm new to Codegniter so go easy on me. I'm building a simple login form and I have successfully redirected to a page when the login credentials are correct. However, if I submit an empty form I get no error messages. I'm also using `set_value` in the form field and codeigniter does not refill what the user inputted once the form is submitted. Somehow that data is being cleared. Here are a few things i've done for clarity sake. - Auto-loaded the form_validation library - Auto-loaded form helper - Echoed `validation_errors` above form account.php (controller) ``` <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Account extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('User_model', 'user'); } public function index() { $this->load->view('home'); } public function validate() { $this->form_validation->set_rules('username', 'username', 'xss_clean|required'); $this->form_validation->set_rules('password', 'password', 'xss_clean|required|md5'); $user_data = array( 'username' => $this->input->post('username'), 'password' => MD5($this->input->post('password')) ); if ($this->form_validation->run() == FALSE) { $data['title'] = "Login Fool"; $this->load->view('templates/header', $data); $data['contents'] = $this->load->view('login_view', $data, TRUE); $this->load->view('page', $data); $this->load->view('templates/footer'); } else { $validated = $this->user->validate($user_data['username'], $user_data['password']); if($validated){ redirect(base_url() . 'account'); } else{ $this->session->set_flashdata('LoginError', 'Sorry, your credentials are incorrect.'); redirect(base_url() . 'account/login'); } } } public function login() { $data['title'] = "Login Fool"; $this->load->view('templates/header', $data); $data['contents'] = $this->load->view('login_view', NULL, TRUE); $this->load->view('page', $data); $this->load->view('templates/footer'); } } ``` login_view.php (view) ``` <h1>Login Now</h1> <?php if(validation_errors() != false) { echo "<div id='errors'>" . validation_errors() . "</div>" ; } ?> <?= ($this->session->flashdata('LoginError')) ? '<p class="LoginError">' . $this->session->flashdata('LoginError') . '</p>' : NULL ?> <?php echo form_open('account/validate'); ?> <label for="username">Username:</label> <input type="text" size="20" id="username" name="username" value="<?php echo set_value('username'); ?>"/> <br/> <label for="password">Password:</label> <input type="password" size="20" id="passowrd" name="password" value="<?php echo set_value('password'); ?>"/> <br/> <input type="submit" value="Login"/> </form> ``` Any ideas why the errors won't show above the form?

Original source