This is a migrated thread and some comments may be shown as answers.

How to programmatically set messages for multiple custom rules?

3 Answers 284 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 14 Feb 2012, 10:32 PM
The documentation is a bit spare on how custom rules should be handled in Validator. E.g., I am assuming that if you have multiple custom rules, they are all handled via decision trees in the "custom" configuration object. And if that is true, then I am assuming you have to do the same for the "message" configuration object if you want to have a different message for each custom rule.

However, it is not clear in the documentation how one should send the specific message text.

E.g., let's say we want to have two custom rules. 

  • One rule checks the "firstname" input field and fails if the value does not include "Tom".  On failure, we want the message for that field to be "Please be a Tom".
  • The other rule checks the "lastname" input field and fails if the value does not include "Smith". On failure, we want the message for that field to be "Please be a Smith".
Based on what examples I can find in the forums, my first guess at how one would do this looks like the following:

$("#myform").kendoValidator({
    rules: {
        custom: function(input) {
            var ret = true;
            if(input.is("[name=firstname]")){
                ret = (input.val() === "Tom");
            }
            else if(input.is("[name=lastname]")){
                ret = (input.val() === "Smith");
            }
            return ret;
        }
        },
    messages: {
        custom: function(input) {
            if( input.is("[name=firstname]") ) {
                    // want the message to say "Please be Tom"
                    return 'Please be a Tom'; // this does not work
                }
                else if ( input.is(["name=lastname]") ) {
                    // want the message to say "Please be Tom"
                    return 'Please be a Smith'; // this does not work
                }
            }
        }
});

The above code is for illustration and has not been actually tested.

However, I do know that the line  return 'Please be Tom'; fails to place any text in the error message.

My hunch is that there is a specific formatting function that must be used. Any hints or leads, here, for what that might be?

TIA

3 Answers, 1 is accepted

Sort by
0
Jay
Top achievements
Rank 1
answered on 14 Feb 2012, 11:21 PM
Well I suppose my cheeks should be red. An alternate reading of the Validator documentation reveals that you can use any key name when creating rules and messages. I.e., all custom rules do not need to be within the "custom" key. 

E.g., in the example I listed above, the proper way to do it would be to create separate keys for each rule. To wit (again, the following is not tested code; it is used for illustration):

$("#myform").kendoValidator({
    rules: {
        firstnametom: function(input) {
            var ret = true;
            if(input.is("[name=firstname]")){
                ret = (input.val() === "Tom");
            }
            return ret;
        },
         lastnamesmith: function(input) {
            var ret = true;
            if(input.is("[name=lastname]")){
                ret = (input.val() === "Smith");
            }
            return ret;
        }
    },
    messages: {
        firstnametom: 'Please be a Tom',
        lastnamesmith: 'Please be a Smith'
    }
});

This solves an immediate problem, though I am academically curious as to how one would address the original example.

Note to Telerik:

I may be the only person who has not immediately grasped that arbitrary key names are allowed in the rules and message objects, but if I am not, you could likely avoid future questions of this sort simply by changing the name of the "custom" key in your documentation examples to something more obviously arbitrary. E.g.

$("#myform").kendoValidator({
     rules: {
         mustContainTom: function(input) {
             return input.is("[name=firstname]") && input.val() === "Tom"; // Only Tom will be a valid value for FirstName input
         }
     }
});

Perhaps it is unnecessary. But I would argue that absent more extensive documentation it could make a notable difference. The Kendo UI suite is notable for its consistency and deliberateness. An unsophisticated or rushed user may assume a level of literalness in the examples that does not apply.


0
David
Top achievements
Rank 1
answered on 15 Feb 2012, 11:02 AM
Thanks for the update, and yes - you were not the only one misreading that ;)
0
Michael
Top achievements
Rank 1
answered on 30 Oct 2013, 10:27 AM
Agreed - documentation is not terribly clear :)

Thanks for this.
Tags
Validation
Asked by
Jay
Top achievements
Rank 1
Answers by
Jay
Top achievements
Rank 1
David
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Share this question
or