Jquery .ajax()和codeigniter形式验证器:为什么codeigniter总是返回true?

时间:2011-02-28 06:53:50

标签: jquery ajax codeigniter validation


我不明白为什么codeigniter form validator总是返回true。对于测试,我正在检查别名元素值是否等于test。我在alias元素中输入test,jquery代码执行ajax。我在firebug的post标签中检查了post值,它说:alias = test但是,codeigniter也返回true。当它应该返回false时,为什么它返回true?任何帮助是极大的赞赏。 -Thanks
   codeignitor代码:

<?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代码: $(document).ready(function(){     function validateElement(formId,element,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')
}); 

});

1 个答案:

答案 0 :(得分:2)

您似乎在alias check

中有validate_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(){}