dtrn74832 2017-04-08 19:05
浏览 111

Codeigniter表单验证不设置规则

I have been stuck for a while trying to setup my validation rules, on my controller I have this:

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

I also have an extended base controller class and an extended form validation class which are as follows:

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

But the problem is every time I try to test the form validation, it always returns false regardless if I have entered some value or not. Any idea what is causing this bug?

By the way, I also tried using CI_Controller only for my controller class, but still no luck

  • 写回答

1条回答 默认 最新

  • doubo6658 2017-04-08 22:30
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行