2017-04-04 97 views
0

我試圖從Codegniter的文檔複製,但我無法使表單驗證回調工作。Codeigniter - 我的表單驗證回調不起作用

我添加了助手窗體,URL和庫form_validation。它不工作,總是返回 「假」

控制器

public function addtest() 
     { 
      $this->load->helper(array('form', 'url')); 

      $this->load->library('form_validation'); 

      $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); 
      if($_SERVER['REQUEST_METHOD'] == 'POST') 
      { 
       if ($this->form_validation->run() == TRUE) 
       { 
         die('true'); 
       } 
       else 
       { 
         die('false'); 
       } 
      } 

      $this->template 
       ->build('myform',array()); 
     } 

    public function username_check($str) 
    { 
     if ($str == 'test') 
    { 
      return TRUE; 
    } 
    else 
    { 
      return FALSE; 
    } 
    } 

查看

<form method="post" action="" class="form-horizontal form-label-left"> 
    <div class="col-xs-12 col-md-9"> 
     <div class="x_panel"> 
      <div class="form-group col-xs-12"> 
       <div class="col-xs-3"> 
        <label class="control-label">Folder name</label> 
       </div> 
       <div class="col-xs-9"> 
        <input type="text" name="username" value="" class="form-control " id="" placeholder=""> 
       </div> 
      </div> 
      <button type="submit" class="btn btn-success">Submit</button> 
     </div> 
    </div> 
</form> 
+1

嘗試添加'$ this'在'$這個 - > form_validation->運行($這個);' – Gaurav

+0

我已經添加了$ this,但是它再次返回false –

+0

您可以嘗試通過刪除請求方法檢查...? –

回答

1

擴展您的form_validation庫Libraries.php

MY_Form_validation.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Form_validation extends CI_Form_validation 
{ 
    function run($module = '', $group = '') { 

     (is_object($module)) AND $this->CI =& $module; 
     return parent::run($group); 
    } 
} 
/* End of file MY_Form_validation.php */ 
/* Location: ./application/libraries/MY_Form_validation.php */ 
0

我的控制器功能是這樣的,它運行完美。我已經自動加載的所有庫

public function change_password() 
{ 
    if($this->isLoggedin()){ 
     $data['title']='Change Password'; 
     if($_POST) 
     { 
      $config=array(
       array(
        'field' => 'old_password', 
        'label' => 'Old Password', 
        'rules' => 'trim|required|callback_checkPassword' 
       ), 
       array(
        'field' => 'password', 
        'label' => 'Password', 
        'rules' => 'trim|required' 
       ), 
       array(
        'field' => 'conf_password', 
        'label' => 'Confirm Password', 
        'rules' => 'trim|required|matches[password]' 
       ) 
      ); 
      $this->form_validation->set_rules($config); 
      if ($this->form_validation->run() == false) 
      { 
       // if validation has errors, save those errors in variable and send it to view 
       $data['errors'] = validation_errors(); 
       $this->load->view('change_password',$data); 
      } 
      else 
      { 
       // if validation passes, check for user credentials from database 
       $this->Login_model->updatePassword($_POST['password'],$this->session->userdata['id']); 
       $this->session->set_flashdata('log_success','Congratulations! Password Changed'); 
       redirect(base_url() . 'Login/dashboard'); 
      } 

     } 
     else 
     { 
      $this->load->view('change_password',$data); 
     } 
    } 
    else 
    { 
     redirect(base_url().'Login'); 
    } 

} 

public function checkPassword($str) 
{ 
    $check=$this->Login_model->checkPassword($str); 
    if($check) 
    { 
     return true; 
    } 
    else 
    { 
     $this->form_validation->set_message('checkPassword', 'The Current Password you have provided is incorrect'); 
     return false; 
    } 
} 
0

在HTML(添加ID字段)

<input type="text" name="username" value="" class="form-control " id="username" placeholder=""> 

<button type="submit" class="btn btn-success" id="submit">Submit</button> 

在你的AJAX代碼

<script type="text/javascript"> 
    $(function(){ 
     $("#submit").click(function(event) 
     { 
      event.preventDefault(); 
      var username= $("#username").username(); 

      $.ajax(
       { 
        type:"post", 
        url: "<?php echo base_url(); ?>index.php/controller/Method", 
        data:{ username:username}, 
        success:function(res) 
        { 

        } 
       }); 
     }); 
    }); 
</script>> 

在控制器

無需檢查if($_SERVER['REQUEST_METHOD'] == 'POST')的CZ其自帶througu它總是

public function addtest() 
{ 
    $this->load->helper(array('form', 'url')); 

    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); 

     if ($this->form_validation->run() == TRUE) 
     { 
      die('true'); 
     } 
     else 
     { 
      die('false'); 
     } 

    $this->template->build('myform',array()); 
} 


public function username_check($str) 
{ 
    if (empty($str)) 
    { 
     echo "Empty"; 
    } 
    else 
    { 
     if ($str == 'test') 
     { 
      return TRUE; 
     } 
     else 
     { 
      return FALSE; 
     } 
    } 
} 

檢查更多Codeigniter CallBack Functon