2016-10-09 55 views
1

任何人都可以幫助我爲什麼我的代碼返回if ($this->form_validation->run() == FALSE)。我是codeigniter中的begginer,我想使用form_validation來匹配更改密碼的規則。我只想要將語句form_validation返回爲true,以便我可以繼續更新數據庫。codeigniter form_validation返回false

我已經加載了form_validation,url等 我的callback_old密碼代碼工作正常,所以我不需要在我的問題中覆蓋它。所以永遠不要回調它已經返回true。

我想要的是專注於我的密碼匹配嗎?我認爲這是做錯了。任何人都可以幫我找出發生了什麼事。 ?根據我聲明的規則將form_validation返回true。

這裏是我的形式:

<form id="pass_form" class="form-horizontal"> 

    <?php $userid = ($this->session->userdata['logged_in']['user_id']); ?> 
                 <input type="hidden" name="userid" id="userid" value="<?php echo $userid ?>" /> 

    <div class="form-group"> 
    <label class="control-label col-sm-3" for="curPword">Current Password</label> 
    <div class="col-sm-6"> 
    <input type="password" class="form-control" name="curPword" id="curPword" placeholder="Enter current password" required /> 
    </div> 
    </div> 

    <div class="form-group"> 
    <label class="control-label col-sm-3" for="newPword">New Password</label> 
    <div class="col-sm-6"> 
    <input type="password" class="form-control" name="newPword" id="newPword" placeholder="Enter new password" required /> 
    </div> 
    </div> 

    <div class="form-group"> 
    <label class="control-label col-sm-3" for="confPword">Confirm Password</label> 
    <div class="col-sm-6"> 
    <input type="password" class="form-control" name="confPword" id="confPword" placeholder="Confirm new password" required /> 
    </div> 
    <div class="col-sm-8 ajax_pass_result"></div> 
    </div> 
    <button onclick="changepass(event)" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Password</button> 
</form> 

我的ajax:

function changepass(e) { 
    e.preventDefault(); 
    jQuery.ajax({ 
    type: "POST", 
    url: "<?php echo site_url('manager/profile/update_password') ?>",  
    data: $("#pass_form").serialize(), 
    success: function(res) { 
     $(".ajax_pass_result").html(res); 

    } 
    }); 

} 

和我的控制器,其中form_validation始終返回false:

public function update_password() { 


     $config = array(
      array(
       'field' => 'curPword', 
       'label' => 'curPword', 
       'rules' => 'trim|required|callback_oldpassword_check' // Note: Notice added callback verifier. 
      ), 
      array(
       'field' => 'confPword', 
       'label' => 'confPword', 
       'rules' => 'trim|required|matches[password]' 
      ), 
      array(
       'field' => 'newPword', 
       'label' => 'newPword', 
       'rules' => 'trim|required' 
      )); 
     $this->form_validation->set_rules($config); 

     if ($this->form_validation->run() == FALSE) { 
       echo 'Error in password fields !'; 
     } 
     else { 
       unset($_POST); 
       $message = "No Error "; 
       echo "<script type='text/javascript'>alert('$message');</script>"; 
      } 
    } 
+0

ü需要使用鑑於這將告訴你你得到了什麼。 – devpro

+1

你已經陷入了使用壓縮詞(因爲想要更好的詞)然後回落到使用完整單詞的陷阱......在你的規則中,你有條目匹配[密碼],密碼在哪裏?它不應該是curPword或currentPassword,它讀取更好。需要額外的按鍵來拼出完整的單詞不(不)花費你的任何東西... – TimBrownlaw

+0

@TimBrownlaw謝謝你的回答,我該如何改變它的先生? 。我不明白什麼hppen ..你能回答嗎? –

回答

3

你只需要改變規則確認密碼驗證爲:

array('field' => 'confPword', 'label' => 'confPword', 'rules' => 'trim|required|matches[curPword]'), 

什麼實際使用的是,你正在使用matches[password],但您的密碼字段名是matches[curPword]

+0

非常感謝您先生..我已經在您身上學到了一些東西。 echo validation_errors(); –

+0

@ jc-john很樂意幫助你 – devpro