2011-02-28 89 views
0


我不明白爲什麼codeigniter表單驗證器總是返回true。對於測試,我正在檢查別名元素值是否等於測試。我將測試輸入到別名元素中,jQuery代碼執行ajax。我在firebug的post選項卡中檢查了post值,它說:alias = test但是,codeigniter也返回true。爲什麼當它返回false時它會返回true?任何幫助是極大的讚賞。 -Thanks
codeignitor代碼:Jquery .ajax()和codeigniter表單驗證器:爲什麼codeigniter總是返回true?

<?php 
class Validate_livestock extends Controller 
{ 
    //create a variable to be used for an array to pass back to the livestock form if there are errors. 
    var $validate_field; 

    function Validate_livestock() 
    { 
     parent::Controller(); 

     //echo 'species11: '.$fs; 

     //turn the validation_field variable into an array 
     $this->validate_field = $this->uri->uri_to_assoc(3); 

    } 

    function validate_form() 
    { 

     //load the form validation library 
     $this->load->library('form_validation'); 

     //set form validation rules 
     switch ($this->validate_field['field']){ 
     case "alias": 
      $this->form_validation->set_rules('alias', 'Alias', 'callback_alias_check', 'trim|xss_clean'); 
      //echo 'input: ' . $this->input->post('alias'); 
     break;  
     } 

     //see if the form validates 

     if ($this->form_validation->run() == FALSE) 
     { 
      //print_r($this->validate_field['field']); 
      $this->load->view('false'); 
      //echo $this->form_validation->run(); 
      //echo "Validator Successfully ran."; 
     } 
     else 
     { 
      //print_r($this->validate_field['field']); 
      $this->load->view('true'); 
      //echo $this->form_validation->run(); 
      //echo "Validator Unsuccessfully ran."; 
     } 

     function alias_check($str) 
     { 
      if ($str == 'test') 
      { 
       return FALSE; 
      } 
      else 
      { 
       return TRUE; 
      } 
     } 
    } 
    } 
} 
?> 

jQuery代碼: $(文件)。就緒(函數(){ 函數validateElement(formId,元件,errorContainer) {

$.ajax({ 
     type: 'POST', 
     cache: false, 
     url: "validate_livestock/" + "validate_form/field/" + element, 
     data: element+"="+$('#'+element).val(), 
     context: document.body, 
     dataType: 'html', 
     success: function(){ 
      alert(document.body) 
     } 
    }); 
} 

$('#alias').change(function(){ 
    validateElement('#add_livestock', 'alias', '#alias_error_1') 
}); 

}) ;

回答

2

你似乎有功能,alias checkvalidate_form method

function validate_form() 
    { 

     //load the form validation library 
     $this->load->library('form_validation'); 

     //set form validation rules 
     switch ($this->validate_field['field']){ 
     case "alias": 
      $this->form_validation->set_rules('alias', 'Alias', 'callback_alias_check', 'trim|xss_clean'); 
      //echo 'input: ' . $this->input->post('alias'); 
     break;  
     } 

     //see if the form validates 

     if ($this->form_validation->run() == FALSE) 
     { 
      //print_r($this->validate_field['field']); 
      $this->load->view('false'); 
      //echo $this->form_validation->run(); 
      //echo "Validator Successfully ran."; 
     } 
     else 
     { 
      //print_r($this->validate_field['field']); 
      $this->load->view('true'); 
      //echo $this->form_validation->run(); 
      //echo "Validator Unsuccessfully ran."; 
     } 

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

內你應該有它的那個方法外:

function validate_form() {} 
function alias_check(){} 
+0

太棒了!謝謝! – dottedquad 2011-02-28 16:13:23