0

I create a login controller and loaded all necessary library and helper when I try to validate my login form it shows fatal error undefined validation_errors()

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LoginController extends CI_Controller{

public function index()
{
    $error['error']='';
    $this->load->library('form_validation');
    $this->load->helper(array('form','url'));

    $this->form_validation->set_rules('user','Username','required|trim');
    $this->form_validation->set_rules('password','Password','required|trim');

    if($this->form_validation->run())
    {
        echo 'success';
    }
    else
    {
        $error['error']='username required';
        return redirect('welcome/login','refresh',$error);
    }
}
}

this is my view

<div class="container">    

<?php echo validation_errors()?>
<div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3"> 

<div class="row">                
<div class="iconmelon">
  <svg viewBox="0 0 32 32">
    <g filter="">
      <use xlink:href="#git"></use>
    </g>
  </svg>
</div>
</div>

<div class="panel panel-default" >
<div class="panel-heading">
    <div class="panel-title text-center">Bootsnipp.com<?php echo $error;?> </div>
</div>     
1
  • Try my answer .hope it will help you Commented Aug 21, 2017 at 12:14

4 Answers 4

1

Method 1 : Load the library in autoload file in this path application\config\autoload.php

$autoload['libraries'] = array('form_validation');

Method 2 : Load the library in controller constructor function .

$this->load->library('form_validation');

1st : Instead of redirect you need to load the view

return redirect('welcome/login','refresh',$error);

To

$this->load->view('welcome/login');
Sign up to request clarification or add additional context in comments.

Comments

1

Check in docs how the flow should look like:

if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
    $this->load->view('formsuccess');
}

In this else block you can set redirection to success page. If form fails you have to load same (in your case welcome/login) view again.

So, your code would be like:

<?php defined('BASEPATH') or exit('Come over next time');

class Logincontroller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $error['error']='';
        $this->load->library('form_validation');
        $this->load->helper(array('form','url'));

        $this->form_validation->set_rules('user','Username','required|trim');
        $this->form_validation->set_rules('password','Password','required|trim');

        if($this->form_validation->run() == false) {
            // you don't need to initialize error, CI will handle that
            $this->load->view('welcome/login');
        }
        else {
            echo 'success';
        }
    }

Comments

0

You should autoload form_validation library.

This should be the path : application\config\autoload.php

Inside that $autoload['libraries'] = array();

And then add form_validation as one value inside this array.

Give it a try. It should work.

1 Comment

I recommend you to debug run method of form_validation library. Check what's going on, where its stuck, then only you come to know the exact issue.
0

Try This

First letter of class and file name should be the only one that is upper case. Also make sure you have set your base url in the config.php read the comment above the $config['base_url']

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

class Logincontroller extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper(array('form','url'));
    }

    public function index()
    {
        $data['error'] = '';

        // You can add other data also.

        $this->form_validation->set_rules('user','Username','required|trim');
        $this->form_validation->set_rules('password','Password','required|trim');

        if($this->form_validation->run() == FALSE)
        {

            $this->load->view('login_view', $data);
        }
        else
        {
            // You can redirect to success controller
            redirect('welcome');
        }
    }

}

view

<?php echo validation_errors();?>

<?php echo form_open('logincontroller');?>
// Form Content
<?php echo form_close();?>

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.