【问题标题】:Form validation in codeigniter always return falsecodeigniter 中的表单验证总是返回 false
【发布时间】:2017-01-30 08:48:07
【问题描述】:

我是codeigniter 的新手,我不明白为什么我的form_validation 一直返回false。这是我的代码:

登录控制器

class Login extends CI_Controller {

     function __construct()
     {
       parent::__construct();
       $this->load->helper(array('form','url'));
       $this->load->library('form_validation');
       $this->load->library('session');
       $this->load->model('user','',TRUE);
     }

     function index()
     {
        $this->load->view('login_view');
     }

     function user_registration_show(){

        $this->load->view('registration_view');

     }

     function user_login_process(){


     }

     function user_registration_process(){

        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('gender', 'Gender', 'trim|required|xss_clean');
        $this->form_validation->set_rules('address', 'Address', 'trim|required|xss_clean');
        $this->form_validation->set_rules('emailadd', 'Email Address', 'trim|required|xss_clean');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password1', 'Re-type Password', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('registration_view');
        } else {

            $pass = $this->input->post('password');
            $pass1 = $this->input->post('password1');
            if($pass!=$pass1){

                $data['message_display'] = 'Password mismatched!';
            }else{
                $data = array(
                'Fname' => $this->input->post('Fname'),
                'Lname' => $this->input->post('Lname'),
                'Gender' => $this->input->post('Gender'),
                'Address' => $this->input->post('Address'),
                'EmailAdd' => $this->input->post('EmailAdd'),
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password')
                );
            }

        }
     }
}

registration_view

<?php echo form_open('login/user_registration_process'); ?>
<div class="content-box-wrapper">
        <div class="form-group">
             <div class="input-group">
                   <input type="text" class="form-control" name="fname" placeholder="First Name" >
              </div>
        </div>
        <div class="form-group">
             <div class="input-group">
                   <input type="text" class="form-control" name="lname" placeholder="Last Name" >
              </div>
        </div>
        <div class="form-group">
             <div class="input-group">
                  <select class="form-control" name="gender" >
                        <option disabled="disabled">Gender</option>
                        <option value="Male">Male</option>
                        <option value="Female">Female</option>
                   </select>
              </div>
         </div>
         <div class="form-group">
              <div class="input-group">
                   <input type="text" class="form-control" name="address" placeholder="Address">
              </div>
          </div>
          <div class="form-group">
               <div class="input-group">
                    <input type="email" class="form-control" name="emailadd" placeholder="Email Address" >
                </div>
           </div>
           <div class="form-group">
                <div class="input-group">
                     <input type="text" class="form-control" name="username" placeholder="Username" >          
                 </div>
            </div>
            <div class="form-group">
                 <div class="input-group">
                      <input type="password" class="form-control" name="password" placeholder="Password" >
                  </div>
             </div>
             <div class="form-group">
                  <div class="input-group">
                       <input type="password" class="form-control" name="password1" placeholder="Re-type Password" >
                  </div>
              </div>
              <input type="submit" class="btn btn-blue-alt btn-block" value="Register"/>        
</div>
<?php echo form_close(); ?>

我正在使用 codeigniter 3.1.3。谁能帮帮我?

【问题讨论】:

  • HTML 表单上的其他必填字段(如性别地址等)在哪里?
  • 哦,先生,我没有粘贴它,但我在我的原始代码中有它
  • 检查表单字段名称应该与'set_rules('name')'匹配。
  • 先生,我已经检查过了,但什么也没发生,它总是返回 false。我已经在 html 表单中包含了其他代码
  • @pryxen 使用 xss_clean 你必须加载安全助手谷歌它,你会找到解决方案。

标签: php codeigniter


【解决方案1】:

由于在所有字段中使用 xss clean check,您的表单验证失败。如果您删除它,它会验证您的字段。因为您的数据已经过 xss 清理(假设 global_xss_filtering 在配置中为 TRUE)

【讨论】:

【解决方案2】:

我认为使用 $this-&gt;input-&gt;post() 获取值时字段名称不匹配。所以尝试这样..

$data = array(
                'Fname' => $this->input->post('fname'),
                'Lname' => $this->input->post('lname'),
                'Gender' => $this->input->post('gender'),
                'Address' => $this->input->post('address'),
                'EmailAdd' => $this->input->post('emailadd'),
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password')
              );

【讨论】:

  • 感谢您的回复先生,但那行代码一开始就没有执行,因为form_validation 返回 false
  • 而不是使用 if 条件匹配密码,使用 required|matches[password] 作为 password1 字段。
  • 谢谢你的建议@Hek-mat 我会做的:)
  • 还可以将规则设置为数组格式,因为它们易于查看和修改。希望您能做到。
【解决方案3】:

请检查控制器中的所有错误。这将返回错误原因。

<?php echo validation_errors(); ?>

 echo "<pre>";

    print_r($this->form_validation->get_all_errors());

    echo "</pre>";

这将在控制器中返回所有表单错误,您可以轻松处理您的请求。

【讨论】:

    【解决方案4】:

    在@GauravRai 先生的帮助和@Hek-mat 先生的建议下,这现在是我的代码。

    登录控制器

    function __construct()
         {
           parent::__construct();
           $this->load->helper(array('url','form'));
           $this->load->library('form_validation');
           $this->load->library('session');
           $this->load->helper('security');
           $this->load->model('user');
         }
    
         function index()
         {
            $this->load->view('login_view');
         }
    
         function user_registration_show(){
    
            $this->load->view('registration_view');
    
         }
    
        function user_registration_process(){
    
            $this->form_validation->set_rules('fname', 'First Name', 'required|xss_clean');
            $this->form_validation->set_rules('lname', 'Last Name', 'required|xss_clean');
            $this->form_validation->set_rules('gender', 'Gender', 'required|xss_clean');
            $this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
            $this->form_validation->set_rules('emailadd', 'Email Address', 'required|xss_clean');
            $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
            $this->form_validation->set_rules('password1', 'Re-type Password', 'required|xss_clean|matches[password]');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('registration_view');
            } else {
    
    
                    $data = array(
                    'Fname' => $this->input->post('fname'),
                    'Lname' => $this->input->post('lname'),
                    'Gender' => $this->input->post('gender'),
                    'Address' => $this->input->post('address'),
                    'EmailAdd' => $this->input->post('emailadd'),
                    'username' => $this->input->post('username'),
                    'password' => MD5($this->input->post('password'))
                  );
    
                $result = $this->user->registration_insert($data);
    
                if($result){
    
                    $data['message_display'] = 'Registered Successfully! Please Login to your account.';
                    $this->load->view('login_view', $data);
                }else{
    
                    $data['message_display'] = 'Username already exist!';
                    $this->load->view('registration_view', $data);
                }
    
            }
         }
    

    我添加了$this-&gt;load-&gt;helper('security');,以便xss_clean 验证我的表单。我还添加了matches[password] 验证以匹配我的password 字段和Re-type Password 字段。谢谢大家帮我解决这个问题:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2016-10-28
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多