I am using codeigniter
if ($this->form_validation->run() ) always returns false.
please help me out with this
Here is my controller and view page as shown bellow

This is controller admin.php


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

class Admin extends CI_Controller {


    public function index()
    {
        $this->login();
    }

    public function login()
    {
        //echo 'This is admin controller login function';
        $this->load->view('admin/admin_login');
    }

    public function login_validation()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');


        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|sha1');

        $email = $this->input->post('email');
        $password = $this->input->post('password');
        print $password . $email;

        if ($this->form_validation->run() == TRUE)
        {
            //redirect('admin/profile');
            $this->load->view('success');
        }
        else
        {
            $this->load->view('errorrr');


        }

    }
}




admin_login.php is the view

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

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Admin Login</title>
      <link rel="stylesheet" media="screen" href="<?=site_url('assets/css/bootstrap.min.css');?>" />
   </head>
   <body>
      <div class="container">
         <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="panel panel-info" >
               <div class="panel-heading">
                  <div class="panel-title">Admin Login</div>
                  <div style="float:right; font-size: 80%; position: relative; top:-10px">
                  <a href="#" onClick="$('#loginbox').hide(); $('#forgotbox').show()">Forgot password?</a></div>

               </div>
               <div style="padding-top:30px" class="panel-body" >
                  <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                  <?php
                     $form_attributes = array(
                        'class' => 'form-horizontal', 
                        'id' => 'loginform',
                        'role' => 'form'
                        );
                     echo form_open('admin/login_validation', $form_attributes);
                     echo validation_errors();
                  ?>
                     <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                       <?php 
                        $email_attributes = array(
                        'id'=>'login-username',
                        'class' => 'form-control',
                        'name'=>'email',
                        'placeholder' => 'Email',
                        'value'=>set_value('email'),
                        'required'=>'required',
                        'placeholder'=>'UserName',
                        );

                        echo form_input($email_attributes);
                       ?>                                     
                     </div>
                     <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                     <?php 
                        $password_attributes = array(
                        'id'=>'login-password',
                        'class' => 'form-control',
                        'name'=>'password',
                        'placeholder' => 'password',
                        'value'=>set_value('password'),
                        'required'=>'required',
                        'placeholder'=>'Password',
                        );

                        echo form_password($password_attributes);  
                     ?> 
                     </div>
                     <div class="input-group">
                        <div class="checkbox">
                           <label>
                           <?php
                              $checkbox_attributes = array(
                               'name'        => 'remember',
                               'id'          => 'login-remember',
                               'value'       => '1',
                               'checked'     => TRUE,
                              );
                              echo form_checkbox($checkbox_attributes);
                           ?>
                             Remember me
                           </label>
                        </div>
                     </div>
                     <div style="margin-top:10px" class="form-group">
                        <!-- Button -->
                        <div class="col-sm-12 controls">

                      <?php
                              $submit_attributes = array(
                               'name'        => 'submit',
                               'id'          => 'btn-login',
                               'class'       => 'btn btn-success',
                               'value'       => 'Login',
                              );
                             echo form_submit($submit_attributes);
                     ?>

                        </div>
                     </div>

                   <?php
                  echo form_close();
                  ?>
               </div>
            </div>
         </div>

      <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
      <script type="text/javascript" src="<?php echo base_url();?>assets/js/bootstrap.min.js" ></script>
   </body>
</html>

Dani AI

Generated

Nice catch, . The reason run() kept returning FALSE is that xss_clean is not a supported Form Validation rule in CodeIgniter 3. In CI3, xss_clean lives in the Security library/helper and is meant for sanitizing data, not for deciding whether a field passes validation. Because it is not on the rule list, adding it to set_rules() makes validation fail. See the official CI3 rule reference and Security Helper docs for confirmation: and Security Helper.

If you want to filter input for XSS, do it when you read the POST data (or explicitly via the Security class), and keep validation focused on correctness. For an email field, required|valid_email is enough; then read it with XSS filtering enabled:

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

$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run()) {
    $email = $this->input->post('email', TRUE); // TRUE applies XSS filtering
    $password = $this->input->post('password', FALSE);
    // ...
} else {
    // show form with errors
}

The TRUE flag above is documented in the Input class: Input::post() second parameter. You can also sanitize explicitly with the Security class: $clean = $this->security->xss_clean($raw); (Security class docs).

Two extra tips inspired by your snippet: avoid echoing output before redirects (it can break headers), and do not hash passwords inside validation rules. Instead, validate first, then hash with PHP’s password_hash() (bcrypt/Argon2) when storing credentials: password_hash() manual. This keeps your validation predictable and your auth flow up to date.

ok i got the error
in admin.php i changed
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');

to
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

but why can't i use xss_clean for email id validation?

we have to load security helper
$this->load->helper('security');

then only we can use xss_clean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.