Codeigniter form_validation-> run()返回false

时间:2017-04-09 13:51:38

标签: php codeigniter validation

我知道这个问题在很多时候被问过,我已经看到了答案,但没有一个能解决我的问题。

我有两个 REST API的控制器,在这两个控件中,form_validation总是返回false。当我评论输出验证部分时,我的两个控制器都能正常工作。

这是我的代码。

第一个控制器用于注册。

    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);

}

这是第二个控制器,用于登录

    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);
}

}

我对Codeigniter相对较新,而且我的截止日期已到期。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

第一次更改:

有时您可能希望验证不是源自$ _POST数据的数组。

在这种情况下,您可以指定要验证的数组:

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

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

无论是验证$ _POST数据还是您选择的其他数组,创建验证规则,运行验证和检索错误消息都是一样的。

  

在定义任何验证规则之前,您必须调用set_data()方法。

请看这个链接:

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

第二次变更:

在ApiController控制器中

请从xss_clean删除set_rules。 xss_clean不是原生规则

$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也来自

在身份验证控制器中

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

请阅读本文作为参考:

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

对于安全助手(xss_clean)

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