Codeigniter表单验证不设置规则

时间:2017-04-08 19:05:19

标签: php codeigniter validation

我已经卡住了一段时间试图设置我的验证规则,在我的控制器上我有这个:

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,但仍然没有运气

1 个答案:

答案 0 :(得分:0)

Problem solved, upon hours of testing seems like my POST data was not reaching my server. The fail safe of my htaccess made it look like the rewrite was actually working but it wasn't, it was like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /appmon/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
  ErrorDocument 404 /appmon/index.php
</IfModule>

After trying things without htaccess things worked, properly. Thanks to everyone