2011-02-28 53 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