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):
- // Script
- jQuery.validator.addRuleSets({
- rules: {
- customRule: {
- required: function() {
- if(jQuery('#someField').val() && jQuery('#someOtherField').val()){return false}
- else return true;
- },
- rangelength: [2,5],
- digits: true,
- remote: {
- url: "/url/validateSomething",
- type: "post",
- data: {
- extraData: function() {
- return jQuery('#element').val();
- }
- }
- }
- },
- customRule2: {
- required:true,
- digits: true
- }
- },
- messages: {
- customRule: {
- required: "Some Error Text",
- rangelength: "Some Other Error Text",
- digits: "etc",
- remote: "etc"
- },
- customRule2: {
- required: "Some Error Text",
- digits: "etc",
- }
- }
- });
- // HTML
- <input id="somefield" name="somefield" type="text" validate="do:customRule" />
- <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):
- // DO:
- remote: {
- url: "/url/validateSomething",
- type: "post",
- data: {
- someKey: function() {
- return jQuery(this).val();
- },
- }
- }
- // BUT NOT:
- remote: {
- url: "/url/validateSomething",
- type: "post",
- data: {
- someKey: function() {
- return jQuery('#absoluteReference').val();
- }
- }
- }
- }
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):
- remote: {
- url: "/url/validateSomething",
- type: "post",
- data: {
- extraData: function() {
- return jQuery('#element').val();
- },
- message: {returnedJSONObject.error}
- }
- }
Thanks in advance,
James.