Welcome Guest, Not a member yet? Register   Sign In
AJAX validation?
#1

[eluser]KeyStroke[/eluser]
Hi,

I'm trying to do validation using the validation libray through AJAX. This is the function that my javascript library calls:
Code:
function validate_data()
    {
        $this->_add_form_init(); // this sets all the form rules and field names
        
        if($this->validation->run() == FALSE)
        {    
                        // send back the errors to the page
            $this->output->set_output($this->validation->error_string);
        }
        else
        {
            // data is valid
            return TRUE;
        }
    }

However, every time my scripts calls this function, I get ALL the errors back, whether my data is actually valid or not.

Am I doing this properly? I'm not sending any data to the function ,so maybe that's the problem.


Your help is appreciated Smile
#2

[eluser]xwero[/eluser]
If you don't send any data to the function how would the function know which field you want to validate. In javascript you should do something like (jQuery ajax function parameters)
Code:
method : 'POST',
data : $(this).attr('id')+'='+$(this).val();
url : '/index.php/form/validate_data';

Then you could add a parameter to the _add_form_init for the post key to let the function figure out which rule(s) should be applied.

Another thing is that the return TRUE isn't understood by javascript so you return an empty string or ok to know the field is valid.
#3

[eluser]KeyStroke[/eluser]
Thanks xwero. I'm using jQuery as well.

My form is pretty big. Is there a way to send the whole POST array to the 'validate_data' function?
#4

[eluser]xwero[/eluser]
For that you can use serialize
then you have to do something like
Code:
data : $("form").serialize();
#5

[eluser]KeyStroke[/eluser]
Thanks xwero.

How do I access the data from the 'validate_data' function? is it stored automatically in the POST array in the AJAX request? or do I have to fetch it somehow?

Also, 'the _add_form_init' function you saw accesses the POST array directly, so I don't think sending it anything is needed.




Theme © iAndrew 2016 - Forum software by © MyBB