Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remote validation help

Tags:

jquery

ajax

php

My issue using the validation plugin is with the remote call specifically.

The remote checks to ensure the employee id is valid from the db. If it is valid then it returns a json string with the employee info (first name, last name, supervisor name, and facility name).

The issue is if I return that json string using the complete: function the empid field stays invalid with the error class, and will not allow the form to submit even though everything is valid. If I just return true from the remote call the empID field is valid and the form will submit (assuming the other fields are completed).

Is there a specific parameter in the json callback that needs to be set to true for the remote: call to finish and be true? I am lost as to how to fix this problem, so any help would be much appreciated! See below for the related code.

               var ajax_data = new Object;
            $('#vpnRequest').validate({
                rules: {
                    empID: {
                        //required: true,
                        //minlength: 4,
                        remote: {
                            url: "checkEmpID.php",
                            dataFilter: function(data) { ajax_data = data; return data;},
                            complete: function() {
                                var jsonObj = new Object;
                                jsonObj = jQuery.parseJSON(ajax_data);
                                var success = jsonObj.status;
                                if(success ==  'false'){
                                    //return success;
                                }else if(success ==  'true'){
                                    $('#fName').val(jsonObj.fName);
                                    $('#lName').val(jsonObj.lName);
                                    $('#superName').val(jsonObj.supervisorFName+" "+jsonObj.supervisorLName);
                                    $('#facilityName').val(jsonObj.facilityName);
                                    $('#empID').addClass('stuff');
                                    $('#empID').removeClass('stuff');
                                    //return success;
                                }
                            }
                        }
                    }
                },
                messages: {
                    empID:{
                        required: "This field is required",
                        remote: "Invalid Employee ID"
                    }
                }
            });

PHP file empID checker:

    $string[status] = 'true';
    $string[fName] = ucwords(strtolower($row['empFirstName']));
    $string[lName] = ucwords(strtolower($row['empLastName']));
    $string[supervisorFName] = $superFName;
    $string[supervisorLName] = $superLName;
    $string[facilityName] = $facilityName;
}
$response = json_encode($string);
echo $response;
} else {
$response = json_encode($valid);
echo $response;
}
like image 259
Brian Avatar asked Jan 27 '26 18:01

Brian


1 Answers

Adding to the validator code itself:

if ($.isFunction(param.validateResult)) response = param.validateResult(response);

from the site http://plugins.jquery.com/content/custom-function-handle-returned-data-remote-function appears to work extremely well, as it validates properly and submits the form. If anyone wants to test further to ensure fringe cases that would be great but this should definitely be added to the validator plugin to extend its capabilities.

like image 78
Brian Avatar answered Jan 29 '26 08:01

Brian