How to programmatically set messages for multiple custom rules?

4 posts, 0 answers
  1. Jay
    Jay avatar
    4 posts
    Member since:
    Jan 2012

    Posted 14 Feb 2012 Link to this post

    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
  2. Jay
    Jay avatar
    4 posts
    Member since:
    Jan 2012

    Posted 14 Feb 2012 Link to this post

    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.


  3. Kendo UI is VS 2017 Ready
  4. David
    David avatar
    45 posts
    Member since:
    Feb 2012

    Posted 15 Feb 2012 Link to this post

    Thanks for the update, and yes - you were not the only one misreading that ;)
  5. Michael
    Michael avatar
    2 posts
    Member since:
    Sep 2013

    Posted 30 Oct 2013 Link to this post

    Agreed - documentation is not terribly clear :)

    Thanks for this.
Back to Top
Kendo UI is VS 2017 Ready