开发者

Jquery .ajax() and codeigniter form validator: Why is codeigniter always returning true?

开发者 https://www.devze.com 2023-02-14 00:54 出处:网络
I do not understand why codeigniter form validator is always returning true.For a test, I am checking if the alias element value equals test.I entered test into the alias element and the jquery code

I do not understand why codeigniter form validator is always returning true. For a test, I am checking if the alias element value equals test. I entered test into the alias element and the jquery code executes the ajax. I checked the post value in firebug's post tab and it says: alias=test However, codeigniter is also returning true. Why is it returning true when it should return false? Any help is greatly appreciated. -Thanks

codeignitor code:

<?php
class Validate_livestock extend开发者_如何学Cs 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 code: $(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')
}); 

});


You seem to have the function, alias check inside the 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;
            }
        }
    }
}

You should have it outside that method:

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

精彩评论

暂无评论...
验证码 换一张
取 消