1

I am trying to use the remote method of the jquery validator plugin but even when return "true" is not valid, the field is still red.

I have already tried to put quotation marks as well as without quotation marks. It blinks green, but soon turns red, on the console I'm printing when it enters if or else, and it enters if, but not valid. Like you can see: https://youtu.be/m42-QZVVz_4

var link = "http://localhost/API/index.php/validaemail";

    $("#btn_cadastro").click(function(){
        $("#formcadastro").validate({
            onkeyup: false,
            submitHandler: function(form){
                form.submit();
            },
            rules:{
                nome:{
                    required : true,
                    maxlength: 100
                },
                senha:"required",
                confirmar_senha:{
                    equalTo: "#senha"
                },
                email: {
                    required: true,
                    remote: {
                        url: link,
                        type: "GET",
                        dataType: "json",
                        data: {email: function() {return $("#email").val();}},
                        complete: function(data){
                            if(data.responseJSON.Status == 1){
                                console.log("TRUE");
                                console.log(data.responseJSON);
                                return true; // i try "true" already
                            }else{
                                console.log("FALSE");
                                console.log(data.responseJSON);
                                return false; 
                            }
                        }
                    }
                }

            },
            messages:{
                email:{
                    required:"Insira um email",
                    email: "Insira um email valido",
                    remote: jQuery.validator.format("{0} já cadastrado")
                }
            }
        });
    });

I wish that when it entered true it would let register.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • 1
    You shouldn't call `.validate()` in a click handler. You call it at top level to tell the validator to run when you try to submit the form. – Barmar Sep 12 '19 at 01:42
  • The remote validator is just supposed to return `true` or `false`, not an object. – Barmar Sep 12 '19 at 01:51
  • You also do not need the `data` parameter *unless* you are sending *additional* data. The value of the `email` field is already being sent by default, so your `data` line can safely be removed. Also put the string value of the `url` parameter until you get this working, then attempt to replace with a variable. Typically, you cannot replace parameters with variables inside the `.validate()` method's options without returning them via a function. – Sparky Sep 12 '19 at 13:52
  • You tagged the post with "PHP" and then don't even bother to show the relevant PHP function that you're calling via `remote`. *How* your PHP responds to this Ajax call is the most important part. The fact that you're using `complete` as a `remote` parameter says that you didn't look at any example code. You don't need to do this... that's what the `remote` method in the plugin is already doing for you. You are supposed to `echo` `"true"`, `"false"`, or a JSON encoded string which would fail the validation and become the custom error message. – Sparky Sep 12 '19 at 14:07
  • In the video shows what returns from php. An object with two things: message and status. And yes I saw all the documentation of remote, several times, but with the examples mentioned could not do what I wanted, because my API returns an object and I can not change it, there is no reason why I change my api just to use remote , this same api is for other things in other parts of the application. – Wellington Capoia Sep 13 '19 at 00:36
  • As for the use of complete I saw right here in stackoverflow, as I get something different from what api expects (true or false) I thought would be the best way to work with it, I check what comes from my api and then return what it wait, which would be true or false. – Wellington Capoia Sep 13 '19 at 00:36

0 Answers0