2017-01-30 48 views
2

我是codeigniter的新手,我无法弄清楚为什么我的form_validation一直返回false。这里是我的代码:codeigniter中的表单验证总是返回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(); ?> 

我使用笨3.1.3。谁能帮我?

+2

其他必填字段,如性格地址等HTML表单上? –

+0

哦,我没有粘贴它的先生,但我有我的原始代码 – pryxen

+0

检查表单字段名称应该从'set_rules('name')'匹配。 –

回答

0

在sir @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->load->helper('security');xss_clean来验证我的形式。我还添加了matches[password]验证以匹配我的password字段和Re-type Password字段。谢谢大家帮我解决这个问题:)

0

我觉得是不是在字段名称在获取值使用$this->input->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') 
      ); 
+0

谢谢您的回复,先生,但该行代码从来没有执行过,因为'form_validation'返回false – pryxen

+0

https://www.codeigniter.com/userguide3/libraries/form_validation.html从这里引用。 –

+1

而不是匹配使用条件的密码如果条件为'password1'字段使用'required | matches [password]'。 –

0

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

<?php echo validation_errors(); ?> 

OR

echo "<pre>"; 

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

    echo "</pre>"; 

这将返回控制器的所有形式的错误,你可以轻松地处理您的请求。

3

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

+0

此主题讨论这个问题 - https://forum.codeigniter.com/thread-1192.html – TimBrownlaw