我知道這個問題已經在這裏問了很多次,我已經看到了答案,但沒有一個解決了我的問題。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);
}
}
我是比較新的笨,我今天到期的最後期限下。
任何幫助將不勝感激。
要補充form_validation庫? $這 - >負載>庫( 'form_validation'); – pradeep
我已經在全球範圍內做到了。在自動載入文件中。但是,嘗試將它加載到控制器中。沒有工作 – Qureshi
哪個版本的CodeIgniter?在版本3中,'xss_clean'不再是表單驗證的一部分。 – Sparky