Welcome Guest, Not a member yet? Register   Sign In
Codeigniter with jQuery Form Validation (JSON)
#1

[eluser]Petsoukos[/eluser]
I know that there might be already something out for doing a form validation using Codeigniter with jQuery. I had a problem to solve to today. A registration form that gets sent via jQuery.post() to a CI controller. But my problem was (still is) that I couldn't run checks individually. Maybe I've missed something in the manual. I had 5 fields and the if($this->form_validation->run() == FALSE) checks them all at once. The problem with this was (still is) that I couldn't highlight individual input fields with the problematic input data. So I did a stupid workaround (because I don't know any better):

A little example:

Code:
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|alpha|min_length[4]|max_length[20]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[40]|xss_clean');

if($this->form_validation->run() == FALSE) {

            if(form_error('first_name')) { // If not empty.... yay there was an error!
                $return_data['errors'][] = 'first_name'; // ... I set the field name in the errors array
            }            

            if(form_error('last_name')) {
                $return_data['errors'][] = 'last_name';
            }
}
echo json_encode($return_data);

Then with a little JS script I can highlight my fields:
Code:
var error_st = {'border-width': '1px', 'border-style': 'dashed', 'border-color': 'red' };
var data = $.parseJSON(data);
                if(data.errors) {
                    $.each(data.errors, function(key, value) {
                        $('input[name="'+value+'"]').css(error_st);
                    });
                }

My stupid workaround actually does what I need... to highlight individual erroneous input fields.

I could use a and here and a better solution to my ongoing problem... for now I'll go production with this workaround.

Thanks!

P.S.
Sorry for my childish post but I'm really frustrated. Cheers! Confusednake:
#2

[eluser]phpgeek[/eluser]
Hello there,
If you want, you can consider JQuery tools validator to perform a jQuery validation and field highlighting.
Google it! (this forum didn't allow me to post the link)

They're pretty helpful.

Cheers.
#3

[eluser]Petsoukos[/eluser]
I've got some client based validation already.. But I prefer the server-side method since it doesn't fail if the user for some odd reason has JS disabled.

Thanks for the input anyway Wink




Theme © iAndrew 2016 - Forum software by © MyBB