2017-04-09 60 views
1

我知道這個問題已經在這裏問了很多次,我已經看到了答案,但沒有一個解決了我的問題。Codeigniter form_validation-> run()返回false

我有兩個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); 
} 

}

我是比較新的笨,我今天到期的最後期限下。

任何幫助將不勝感激。

+0

要補充form_validation庫? $這 - >負載>庫( 'form_validation'); – pradeep

+0

我已經在全球範圍內做到了。在自動載入文件中。但是,嘗試將它加載到控制器中。沒有工作 – Qureshi

+0

哪個版本的CodeIgniter?在版本3中,'xss_clean'不再是表單驗證的一部分。 – Sparky

回答

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控制器

請從set_rules刪除xss_clean。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]'); 

,也從

在驗證控制器

$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

˚F或安全助手(xss_clean)

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

+0

假設他用CI版本3 – Sparky

+0

我使用的版本3和我刪除了xss_clean規則,但仍然沒有工作 – Qureshi

+0

使用這樣的:($這個 - > form_validation->的run()== FALSE)和也匹配字段名 – pradeep