jquery-validation icon indicating copy to clipboard operation
jquery-validation copied to clipboard

Question about validating from Meteor method

Open allenfuller opened this issue 9 years ago • 4 comments

I've successfully returned a value from Meteor.call to a method on the server, however now I can't get it to actually validate based on the value returned.

Here's the custom validator:

$.validator.addMethod("orgDomainUnique", function(value, element) {
  Meteor.call( 'checkOrgDomain', value, function( error, result ) {
    console.log('validator method response: ', result);    
    return result;
  });
}, "This URL is already in use. Please pick another.");

It returns false if there is an existing domain and true if there is not one. I've tried it the opposite as well, but regardless, I get an error on the form.

Here's the server method:

let check = ( value ) => {
  var lookupOrg = Orgs.findOne({ "domain": value });
  if ( lookupOrg ) {
    return false;    
  }
  else {
    return true;
  }
};

Modules.server.findOrgDomain = check;

Any idea how to go that last mile on validation?

Thanks!

allenfuller avatar Dec 23 '15 22:12 allenfuller

@allenfuller if you haven't solved this yet, the best way I've found to use this with a method is to rely on a session variable. Return that session from the validator method and set it in the callback of the Meteor.call(). For example:

$.validator.addMethod("orgDomainUnique", function(value, element) {
  Meteor.call( 'checkOrgDomain', value, function( error, result ) {
    if ( error ) {
      console.log( error );
    } else {
      Session.set( 'checkOrgDomain', result );
    }
  });

  return Session.get( 'checkOrgDomain' );
}, "This URL is already in use. Please pick another.");

themeteorchef avatar Jan 31 '16 19:01 themeteorchef

As always, that did the trick -- thanks!

allenfuller avatar Feb 25 '16 15:02 allenfuller

So I've got another issue that I just can't shake with this code. It always returns a "false" false when I first run it. A user types in a unique orgDomain and it comes back with "This URL is already in use." Obviously a little bit of a head scratcher for new users...

It seems the issue of a delayed validation on remote calls is a common issue with the jQuery Validation plugin in general, but I didn't know if you had a Meteor-specific way of making the validation wait until the Meteor method returned a result...

Thanks!

allenfuller avatar May 10 '16 05:05 allenfuller

I have same problem https://stackoverflow.com/questions/46308369/jquery-validation-plugin-always-shows-error-message

Now I try session mode:

$.validator.addMethod('checkEmailUnique', function(value, element) {
    Meteor.call('checkEmailUnique', value, function(err, result) {
      console.log('Email validator method response: ', result);
      Session.set('sessionCheckEmailUnique', result);
      console.log('sessionCheckEmailUnique: ', Session.get('sessionCheckEmailUnique'));
      return Session.get('sessionCheckEmailUnique');
    });

and get same problem.

kuznets avatar Sep 28 '17 17:09 kuznets