0
我一直在堅持了一段時間試圖設置我的驗證規則,我的控制器上我有這樣的:笨表單驗證未設定規則
public function login() {
$this->load->library('form_validation');
$this->form_validation->set_rules(array(
'field'=>'username',
'label'=>'Username',
'rules'=>'required'
));
if (!$this->form_validation->run()) {
$this->json(array(
'status'=>'error',
'errors'=>$this->form_validation->error_array(),
'test'=>$this->form_validation->get_options()
));
return;
}
$user = $this->MUser->findByAttributes(array('username'=>$this->input->post('username')));
if(!$user) {
$this->json(array(
'status'=>'error',
'errors'=>array('username'=>'Username is not yet registered.')
));
return;
}
$encpass = sha1($user->salt.$this->input->post('password'));
if ($encpass != $user->password) {
$this->json(array(
'status'=>'error',
'errors'=>array('password'=>'Invalid username/password, please try again.')
));
return;
}
$this->json(array(
'status'=>'success',
'errors'=>''
));
}
我也有一個擴展的基本控制器類和擴展的表單驗證類如下:
class AM_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function render_template($view, $data = array(), $internal = false) {
$view_contents = $this->load->view($view, $data, true);
$header = $internal?$this->load->view('internal/header',$data,true):$this->load->view('external/header',$data,true);
$footer = $internal?$this->load->view('internal/footer',$data,true):$this->load->view('external/footer',$data,true);
$this->output->set_status_header(200,'ok');
$this->load->view('general/template',array_merge(array(
'header'=>$header,
'footer'=>$footer,
'content'=>$view_contents
),$data));
}
public function json($data) {
$this->output->set_status_header(200,'ok')
->set_content_type('application/json')
->set_output(json_encode($data));
}
}
class AM_Form_validation extends CI_Form_validation {
public function __construct($options = array()) {
parent::__construct($options);
}
public function correct_captcha ($str) {
$this->set_message('correct_captcha','Invalid %s answer.');
return $this->CI->session->userdata('captcha_answer') == $str;
}
public function get_options() {
return $this->_field_data;
}
}
但問題是,每次我試圖測試表單驗證的時候,它總是返回false,如果不管我已經進入一定的價值或沒有。任何想法是什麼造成這個錯誤?
順便說一句,我也嘗試過使用是CI_Controller只爲我的控制器類,但仍沒有運氣
AM_Controller與'index()'在同一個類嗎?如果沒有,什麼是'index()',它是如何被調用的?很難看到你展示的代碼是如何融合在一起的。 – DFriend
如果你在codeigniter中使用了很多AJAX,你會對這個[AJAX庫](https://github.com/joshcam/Codeigniter-jQuery-Ajax)感興趣。與CI配合良好。 – DFriend
@DFriend是所有控制器都使用AM_Controller –