[Validation - bassistance.de] - Rule sets? Remote data / error messages? and a bit of praise.

[Validation - bassistance.de] - Rule sets? Remote data / error messages? and a bit of praise.

First of all, for Jörn, thanks for all the work you put into this plugin; no validation plugin will ever suit everyone's needs but this one comes damn close; excellent work.

Now for the questions.

1. Is it possible to have a custom rule set with custom error messages? preferably one that's called by an attribute value? or what would be the best way to implement that?

For example (something like):

  1. // Script
  2. jQuery.validator.addRuleSets({
  3.     rules: {
  4.         customRule: {
  5.                 required: function() {
  6.                     if(jQuery('#someField').val() && jQuery('#someOtherField').val()){return false}
  7.                     else return true;
  8.                 },
  9.                 rangelength: [2,5],
  10.                 digits: true,
  11.                 remote: {
  12.                     url: "/url/validateSomething",
  13.                     type: "post",                  
  14.                     data: {
  15.                         extraData: function() {
  16.                             return jQuery('#element').val();
  17.                         }
  18.                     }
  19.                 }
  20.         },
  21.         customRule2: {
  22.                 required:true,
  23.                 digits: true
  24.         }
  25.     },
  26.     messages: {
  27.         customRule: {
  28.             required: "Some Error Text",
  29.             rangelength: "Some Other Error Text",
  30.             digits: "etc",
  31.             remote: "etc"
  32.         },
  33.         customRule2: {
  34.             required: "Some Error Text",
  35.             digits: "etc",
  36.         }
  37.     }
  38. });

  39. // HTML
  40. <input id="somefield" name="somefield" type="text" validate="do:customRule" />
  41. <input id="someotherfield" name="someotherfield" type="text" validate="do:customRule2" />

The reason for this is a little complicated but basically it's because I can't count on concrete field names / ids  (or I would simply write the rules based on those). Additionally, I would rather not jam 30 lines of script into an attribute value (or class); plus, I am a firm believer in not over-abusing class names for the sake of script processing even if it doesn't validate (which is why I want to look for an alternative to addClassRules - though it isn't clear whether that supports custom error messages without custom methods either).

2. Is it possible to reference the element being validated in a remote call without doing it absolutely?

For example (something like):

  1. // DO:
  2. remote: {
  3.     url: "/url/validateSomething",
  4.     type: "post",                  
  5.     data: {
  6.         someKey: function() {
  7.             return jQuery(this).val();
  8.         },
  9.     }
  10. }
  11. // BUT NOT:
  12. remote: {
  13.     url: "/url/validateSomething",
  14.     type: "post",                  
  15.         data: {
  16.             someKey: function() {
  17.                 return jQuery('#absoluteReference').val();
  18.             }
  19.         }
  20.     }
  21. }

Again, for the same reason. I can't count on field names / ids and would like to send the server a specific named key/value pair regardless of the actual field name key. (this assumes, of course, that I find a solution to question 1)

3. Is it possible to return a custom error message when using the "remote" rule?

For example (something like):

  1. remote: {
  2.     url: "/url/validateSomething",
  3.     type: "post",                  
  4.     data: {
  5.         extraData: function() {
  6.             return jQuery('#element').val();
  7.         },
  8.     message: {returnedJSONObject.error}
  9.     }
  10. }

Thanks in advance,

James.