Search code examples
phpcodeignitercodeigniter-2codeigniter-3

unable to get form_validation->set_rules and form_validation->set_message error


I have autoload helper form and library form-validation in autoload.php

 <?php
  defined('BASEPATH') OR exit('No direct script access allowed');

 class Login extends CI_Controller {


    var $data;
    public function __construct(){
        parent::__construct();
        $this->data=array('page'=>'login_view','title'=>'login');
           $this->load->model("Login_model");

    }
  public function index()
 {
      //get the posted values
    $data=$this->data;
    $this->load->view('template',$data);

 }
 public function checklogin(){
    //form validation
        $data=$this->data;
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password' ,'Password','required|callback_verifyUser');
            if($this->form_validation->run()==false){
            $this->load->view('template',$data);
        }
        else{

        }
 }
 public function verifyUser(){
    $name=$this->input->post('username');
    $pass=$this->input->post('password');
      $option=$this->input->post('optionlist');

    if($this->Login_model->login($name,$pass,$option)){
        return true;

    }else{
                    //user doesn't exist
           //echo "Wrong username and password";
         $this->form_validation->set_message('verifyUser','Incorrect officer_id or password. Please try again');
        return false;

    }

 }
}

?>

The problem is it doen't show any error message USERNAME IS REQUIRED OR PASSWORD IS REQUIRED WHEN I LEAVE IT BLANK . IT SIMPLY REDIRECTS TO LOGIN_VIEW.PHP WITHOUT SHOWING ANY ERROR MESSAGE THAT USERNAME IS LEFT BLANK

THE doc SAID FORM-VALIDATION IS USED FOR THe PURPOSE to show errors.

for the view login_view.php u can see form here http://pastebin.com/T2zMYGn1


Solution

  • use echo validation_errors() in view file (template.php) where you want to see error messages