3

The jQuery validation plugin (jquery.validate.js) supports the method addClassRules(). This allows me to do something like this:

$.validator.addClassRules({
  birthdate: {
    date: true,
    notFuture: true,
    notBefore1900: true
  }
});

where the notFuture and notBefore1900 are methods I've previously specified with calls to .addMethod().

This allows me to use that by specifying the class: <input class="birthdate" name="foo" /> and it validates just fine, displaying the appropriate validation error message based on the specific method that triggered the invalid state.

My question is how do I do such compound rules and specify them as part of the rules arguments to the validate() method? I want to be able to do this:

$('#myForm').validate({
  rules: {
    foo: 'birthdate'
  }
});

Sadly while validation methods themselves can be represented either as class names or as explicit rules in the arguments like

rules: {
  foo: 'date'
}

one cannot do the same with class rules; it expects the rules arguments to validate() to be a method and not a class rule.

I could do something like:

var reusableRule = {
  date: true,
  notFuture: true,
  notBefore1900: true
};
...
$('#myForm').validate({
  rules: {
    foo: reusableRule
  }
});

but I'd prefer to keep things contained within the plugin itself rather than invading the global scope. I could do something like this

$.validator.customRules = {
  birthdate: {
    date: true,
    ...
  }
}

but then it looks ugly (or at least inconsistent with the syntax of the other rules items):

$('#myForm').validate({
  rules: {
    foo: $.validator.customRules.birthdate
  }
});

Also, I run the risk of a later version of the plugin specifying something like customRules which would cause a collision.

For now I think I'll keep them in the global scope, but if anyone has a cleaner way (or if there's a 'right way' that I missed when parsing the documentation) please share. Thanks :)

Sparky
  • 98,165
  • 25
  • 199
  • 285
jinglesthula
  • 4,446
  • 4
  • 45
  • 79
  • I think the `customRules` solution is your best bet. I've seen this in systems before and it has worked fine across recent version changes so far. – Robb Nov 21 '13 at 17:07
  • yeah... I just hate 'polluting the global namespace' if it can be avoided. It would be nice if the plugin itself supported some method to pass in a string and an object to go with it and it could internally check against those as well as the defined methods. Le Sigh. – jinglesthula Nov 21 '13 at 17:12
  • There are many ways to define and declare rules with this plugin... and you seem to have already tried most of them. – Sparky Nov 22 '13 at 19:40

1 Answers1

1

Quote OP:

"My question is how do I do such compound rules and specify them as part of the rules arguments to the validate() method?"

  • The .addClassRules() method creates compound rules that you can only declare inline as a class.

  • If you want a rule you can declare within .validate(), then you would use the addMethod method.

  • Alternatively, compound rules can be assigned to variables that can be used within .validate().

Otherwise, I really don't see how "invading the global scope" is any kind of a problem worry about. Either you'll use the options as provided, or create your own. I don't see how you could ever customize any option for any plugin without some invasion of "the global scope".


Quote OP:

"Also, I run the risk of a later version of the plugin specifying something like customRules which would cause a collision"

I think that's highly unlikely, and probably impossible. Even when you create a custom method with the .addMethod() method using the same name as an existing method, only the second instance is used... that instance would be your custom rule since it's defined after the plugin's methods are included.


You could also combine your custom compound rules into new sets of compound rules using the jQuery extend() method.

Here is some reference: https://stackoverflow.com/a/9056425/594235

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Yeah, I was just hoping to be able to combine and reuse existing rules with their individual messages and get the behavior of addClassRules by specifying a single string/key in the .validate() argument as opposed to passing an object to .validate() directly. I wish there were an 'addArgumentsRules' or that 'addClassRules' let you use the 'class' in .validate() as a string as well as on the element's class attribute. Oh well - maybe in a later version of the plugin :) – jinglesthula Nov 26 '13 at 20:10
  • As for the collision, I mean that if a later version of the plugin specified something on $.validator that had the same name as something I decided to bolt on as a container for my custom rule definition objects that it would collide if I upgraded. I wouldn't be adding a custom method, but a member of $.validator directly. – jinglesthula Nov 26 '13 at 20:13
  • @jinglesthula, I believe that's a moot point, as in my `addMethod` example, your code would take precedence. But you could just use very unique naming that is unlikely to be duplicated by the developer. – Sparky Nov 26 '13 at 20:20