0

I'm using the jQuery validator's remote method to determine if an email address already exists in our SaaS platform.

The code below is what we are using and it's fine. However, we would like to ideally return some information about the existing user, so that we may display a link to the existing patient's profile.

I've scoured the validator docs and can't seem to find any way to handle return parameters other than true/false. Is this a limitation of the remote method?

jQuery

 form.validate({
      rules: {
           email: {
                remote: {
                    url: "CheckEmailExists.php",
                    type: "post"
                }
           }
      },
      messages: {
           email: {
                remote: "This email address is already assigned to an existing patient"
           }
      },
 });

PHP

$existingUserQuery = "SELECT tbl_patients.patient_id FROM tbl_patients WHERE tbl_patients.patient_email = :email";
$getUser = $pdo->prepare($existingUserQuery);
$getUser->bindparam(":email", $email);
$getUser->execute();
$numCount = $getUser->rowCount();

if ($numCount > 0) {
    echo 'false';
} else {
    echo 'true';
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Adam G
  • 1,188
  • 10
  • 24
  • I think you'll probably have to write a custom validator, not use the built-in `remote:` rule. – Barmar May 21 '20 at 21:35
  • 1
    *"I've scoured the validator docs"* - did you? [If you look at the docs: ***"If the serverside response is a string, eg. "That name is already taken, try peter123 instead", this string will be displayed as a custom error message in place of the default."***](https://jqueryvalidation.org/remote-method/). So instead of `false`, you can return a JSON encoded string which becomes the validation error message. As far as getting more info when it's `true`... not sure why you'd need that since presumably, the user is already part of your system and when logged in, you can send whatever message. – Sparky May 22 '20 at 20:05
  • @Sparky not sure how I missed that. Thank you for the help – Adam G May 23 '20 at 04:06

1 Answers1

1

You can create a custom method and within the method do run your own remote call via AJAX. Then in your php, return a JSON object with the values that you want to use.

jQuery.validator.addMethod("validatemmail", function(value, element) {
    var _is_valid = false;
        var qry = {"email" : value};
        $.ajax({
                    type: "POST",
                    url: "CheckEmailExists.php",
                    data: qry,
                    done: function(data){
                        if(data.error == "new"){
                            _is_valid = true;
                            console.log(data.patient_name);
                        }
                        else{
                            _is_valid = false;
                        }

                       return this.optional(element) || _is_valid );
                    }
                });


    }, "This email address is already assigned to an existing patient");



$('validatorElement').validate({
    rules : {
        email : { validatemmail : true }
    }
});

Your PHP:

$return = [];

if ($numCount > 0) {
    $return["error"] = "exists";
    $return["patient_name"] = "John";
} else {
    $return["error"] = "new";
}

    header('Content-Type: application/json');
    echo json_encode($return);

The original idea: jQuery Validate Plugin - How to create a simple custom rule?

imvain2
  • 15,480
  • 1
  • 16
  • 21