jpkleemans / angular-validate

Painless form validation for AngularJS. Powered by the jQuery Validation Plugin.
MIT License
68 stars 33 forks source link

how to use remote inside form.validate #28

Closed bichitra12 closed 5 years ago

bichitra12 commented 5 years ago

please help $scope.signupsub=function(signup){ if(signup.validate({ rules: { mobile: { remote: {

              url: data+'ajaxcall/mobilechechkuser',

                type: "post"

            }

      },
      email: {

        remote: {

                url: data+'ajaxcall/emailchechkuser',

                type: "post"

            }

      }
    },
    messages: {

        mobile: {

          remote:"mobile number already exit"
          },
          email: {

          remote:"email already exit"
          }
    }
  })){
    alert();
  }
}

remote url is calling but not showing error thanks

bichitra1 commented 5 years ago

pls help some body

jpkleemans commented 5 years ago

Hi! I think it has something to do with the serverside response you're returning from the ajaxcall endpoints.

From the docs:

The serverside response must be a JSON string that must be true for valid elements, and can be false, undefined, or null for invalid elements, using the default error message. 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.

See http://jsfiddle.net/9ruhmex0/ for a working example.

bichitra1 commented 5 years ago

its not working inside validate() function but its working like this $scope.validationOptions = { rules: { email: { required: true, remote: { url: '/echo/json', type: 'post', data: { json: 'false' } } } } };

jpkleemans commented 5 years ago

Ah, I see. That's because the if statement is synchronous and the remote call is asynchronous. The solution is indeed to pass the validation options into the ng-validate directive, instead of the validate() method.

bichitra1 commented 5 years ago

can you tell how

bichitra1 commented 5 years ago

$scope.validationOptions this one?

jpkleemans commented 5 years ago

Yes, $scope.validationOptions is the way to go!

bichitra1 commented 5 years ago

thanks