2017-01-30 152 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