Qureshi
Qureshi

Reputation: 13

Codeigniter form_validation->run() is returning false

I know this question has been asked here a ton of times and I have seen the answers, but none of them solves my problem.

I have two REST API's Controllers and in both of them, the form_validation always returns false. When I comment out the validation section, both my controllers work fine.

This is my code.

The first controller is used for registration.

    class ApiController extends REST_Controller{

public function create_password($password){

    return hash("sha256", $password);
}

public function data_post(){

    $this->form_validation->set_rules(
                                    'username','User Name','trim|required|min_length[5]|max_length[30]|is_unique[users.user_name]|xss_clean');
    $this->form_validation->set_rules('firstname','First Name','trim|required|alpha|min_lenght[3]|max_length[30]|xss_clean');
    $this->form_validation->set_rules('lastname','Last Name','trim|required|alpha|min_lenght[3]|max_length[30]|xss_clean');
    $this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.user_email]');
    $this->form_validation->set_rules('password','Password','trim|required');
    $this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]');
    $this->form_validation->set_rules('gender','Gender','required');
    $this->form_validation->set_rules('dob','Date of Birth','required');
    $this->form_validation->set_rules('phone','Mobile Number','required');



   if($this->form_validation->run() === FALSE){
       $errors = validation_errors();
       $message = array(
                    'status' => FALSE,
                    'message' => $errors

        );
        //$this->response($message, REST_Controller::HTTP_NOT_ACCEPTABLE);
        echo validation_errors();
        return;
    }
    $userpass = $this->create_password($this->post('password'));
    $data = array('user_name'=>$this->post('username'),
                  'first_name'=>$this->post('firstname'),
                  'last_name'=>$this->post('lastname'),
                  'user_email'=>$this->post('email'),
                  'password'=>$userpass,
                  'date_of_birth'=> $this->post('dob'),
                  'mobile_phone' => $this->post('phone'),
                  'user_gender' => $this->post('gender')
    );    

    $recordEntered = $this->mainModel->insert($data);
    $message = '';

    if($recordEntered == 1){

        $message = array(
                    'status' => TRUE,
                    'message' => 'Data Inserted Successfully'

        );
    }

    $this->response($message, REST_Controller::HTTP_CREATED);

}

And this is the 2nd Controller, used for login

    class Authentication extends REST_Controller{

function index_post(){

    $this->form_validation->set_rules('username','User Name','trim|required|max_length[30]|xss_clean');
    $this->form_validation->set_rules('password','Password','trim|required');

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

       $errors = validation_errors();
       $message = array(
                    'status' => FALSE,
                    'message' => $errors

        );
        $this->response($message, REST_Controller::HTTP_NOT_ACCEPTABLE);
        return;
    }
    $username = $this->post('username');    
    $password = $this->create_password($this->post('password'));
    $message = '';

    if($this->verify_user($username, $password)){

        $data = array(
                    'email' => $this->post('username'),
                    'is_logged_in' => 1
                     );
        $this->session->set_userdata($data);
        $message = array(
                    'status' => TRUE,
                    'message' => 'Log In Successful'
                    );
        $this->response($message, REST_Controller::HTTP_OK);
    }
    else{

        $message = array(
                    'status' => FALSE,
                    'message' => 'Invalid Email/Username or Password'
        );
        $this->response($message, REST_Controller::HTTP_NOT_FOUND);     
    }
}
public function verify_user($username, $password){ 

    if($this->mainModel->getUser($username, $password)){

        return true;
    }  
    else{

       return false;
    }
}
public function create_password($password){

    return hash("sha256", $password);
}

}

I am relatively new to Codeigniter and I am under a deadline which expires today.

Any help would be greatly appreciated.

Upvotes: 1

Views: 3765

Answers (2)

Teejaygenius
Teejaygenius

Reputation: 49

removing the xss_clean makes my code works perfectly

Upvotes: 1

Pradeep
Pradeep

Reputation: 9707

First Change :

Sometimes you may want to validate an array that does not originate from $_POST data.

In this case, you can specify the array to be validated:

 $data = array(
    'username' => 'johndoe',
    'password' => 'mypassword',
    'passconf' => 'mypassword'
 );

 $this->form_validation->set_data($data);

Creating validation rules, running the validation, and retrieving error messages works the same whether you are validating $_POST data or another array of your choice.

You have to call the set_data() method before defining any validation rules.

see this link :

https://www.codeigniter.com/user_guide/libraries/form_validation.html#validating-an-array-other-than-post

Second Change :

In ApiController controller

Please remove xss_clean from the set_rules. xss_clean is not a native rule

$this->form_validation->set_rules('username','User Name','trim|required|min_length[5]|max_length[30]|is_unique[users.user_name]');
$this->form_validation->set_rules('firstname','First Name','trim|required|alpha|min_lenght[3]|max_length[30]');
$this->form_validation->set_rules('lastname','Last Name','trim|required|alpha|min_lenght[3]|max_length[30]');

AND Also from

In Authentication controller

  $this->form_validation->set_rules('username','User Name','trim|required|max_length[30]');

Please read this for the reference :

https://www.codeigniter.com/user_guide/libraries/form_validation.html#rule-reference

For security helper ( xss_clean )

https://www.codeigniter.com/user_guide/helpers/security_helper.html

Upvotes: 1

Related Questions