modify standard validation messages

13 posts, 0 answers
  1. Daniel
    Daniel avatar
    192 posts
    Member since:
    Mar 2012

    Posted 21 Mar 2013 Link to this post

    1. How can i change the standard validation messages?
    2.Can i change this messages via Data Adnotations in the viewModel?

    for example in my view model i did not put something in the View model regarding that a field must be of a specific type ,and i get a standard message like :"the field Precision must be of type integer" i accidentally entered a string there.
    So what are the ways of controlling this standard validation messages?

    Regards,
    Daniel
  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 22 Mar 2013 Link to this post

    Hello Daniel,

    You can customize the default messages in the Validator configuration or by data-attributes attached to the corresponding input elements. For more information and examples please check the corresponding documentation.

    In this forum thread you will find samples that demonstrate how to change the messages through the View-Model.
    I hope this will help.

    Kind regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Daniel
    Daniel avatar
    192 posts
    Member since:
    Mar 2012

    Posted 25 Mar 2013 Link to this post

    1.can you give me an example how or where can i change a standard validation message like
    "the field Precision must be of type integer" where this come from?

    2. By ViewModel i was refering to ViewModel on MVC side(server-side),i am trying to use as much as possible,mvc wrappers and server-side,not client-side.
    So i was refering to DataAdnotations from Entity Framework and and ViewModel made from a c# class over the domain model.
    So the 2nd question is about C#,entity framework,mvc,not javascript and client-side programming.
    Again,
    Can i change this messages via Data Adnotations in the viewModel(c# class)?

    Regards,
    Daniel
  5. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 27 Mar 2013 Link to this post

    Hi Daniel,

    Regarding your questions:
    1. The text of the default messages is part of the Kendo source. If you wish, you can examine the src/kendo.validator.js file (search for "messages:"). Changing the messages in the source is not recommended. Please read #2.

    2. Yes it is possible to set the validation message in the C# Model and actually this is the recommended approach in case you use KendoUI for ASP.NET MVC. For example:
      [Required(ErrorMessage="my custom error message")]
      [DisplayName("Product name")]
      public string ProductName
      {
          get;
          set;
      }

    Regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Daniel
    Daniel avatar
    192 posts
    Member since:
    Mar 2012

    Posted 04 Apr 2013 Link to this post

    Ok,but in your example ,if i enter a datetime or a number,so it's not the corect field format,than how can i manage the message for that?
    i was able to put a message for required but i got a message regarding the not correct format,in my case was an integer and i entered a string,so i want to deal also this message too.

    Regards,
    Daniel
  7. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 08 Apr 2013 Link to this post

    Hi Daniel,

    I am not sure if I understood your question correctly - a "format" validation attribute does not exists. It is possible to validate the input against pattern (regular expression).
    [Required(ErrorMessage="my custom error message")]
    [RegularExpression("d{10}", ErrorMessage="Pattern does not match")]
    [DisplayName("Product name")]
    public string ProductName
    {
        get;
        set;
    }

    Also you may create a custom validation rule. In this forum thread you will find a sample project which demonstrates how this can be accomplished.

    Another possible approach for configuring the validation is to extend it:
    (function($, kendo) {
        $.extend(true, kendo.ui.validator, {
            messages: {
            //configure the messages here
            //docs.kendoui.com/api/framework/validator#configuration-messages
            }
        });
    })(jQuery, kendo);

    I hope this will help.

    Kind regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Daniel
    Daniel avatar
    192 posts
    Member since:
    Mar 2012

    Posted 10 Apr 2013 Link to this post

    by format i meant, the right data type.
    You can try the following,if you have an integer field in your data model,and you enter a string,when you want to save , it will appear a message like this:
    "The field xxxx must be a number" ,and this message also i would like to customize with my own message,if it is possible from data adnotations if not,via kendo validators or some javascript.

    Regards,
  9. William Dunn
    William Dunn avatar
    15 posts
    Member since:
    May 2010

    Posted 10 Apr 2013 Link to this post

    I'm having a similar situation with email address.

    My model has:
    [DisplayName("Supervisor email")]
    [Required(ErrorMessage = "Supervisor email address is required")]
    [EmailAddress(ErrorMessage="Supervisor email is not a valid email address")]
    public string SupervisorEmail { get; set; }
    My view has:
    @Html.TextBoxFor(x => x.Supervisor.SupervisorEmail, new { maxlength = 100,
    type="email" })


    The generated html is:
    <input data-val="true" data-val-email="Supervisor email is not a valid email address" data-val-required="Supervisor email address is required" id="Supervisor_SupervisorEmail" maxlength="100" name="Supervisor.SupervisorEmail" type="email" value="" />
    When the client validation fires I get the default "Supervisor_SupervisorEmail is not valid date" instead of the data-val-email text.

    Looking over the kendo documentation I found that data-email-msg should be used so I did the following jquery on document.ready():
    $("#Supervisor_SupervisorEmail").attr("data-email-msg", "Supervisor email is not a valid email address");
    $("#Supervisor_SupervisorEmail").removeAttr("data-val-email");
    This makes the validation work great but is kendo creating the wrong attribute on the html and is my method a suitable alternative?

    I'm using the latest version of kendo for mvc (2013.1.319).

    thanks


  10. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 12 Apr 2013 Link to this post

    Hello William,

    To create validation rule and message I suggest you to follow the approach covered in the MVC Grid demo related to EditingCustomValidation

    For reference I will paste it here:

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CustomValidationProductViewModel>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.UnitPrice).Width(140);
            columns.Command(command => command.Edit()).Width(200);
        })
        .Editable(editable => editable.Mode(GridEditMode.InLine))
        .Pageable()
        .Scrollable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .Events(events => events.Error("error_handler"))
            .Model(model => model.Id(p => p.ProductID))
            .Read(read => read.Action("EditingCustomValidation_Read", "Grid"))
            .Update(update => update.Action("EditingCustomValidation_Update", "Grid"))
        )
    )
    <script type="text/javascript">
     
        //register custom validation rules
        (function ($, kendo) {
            $.extend(true, kendo.ui.validator, {
                rules: { // custom rules
                    productnamevalidation: function (input, params) {
                        //check for the rule attribute
                        if (input.filter("[data-val-productnamevalidation]").length && input.val()) {
                            return /^[A-Z]/.test(input.val());
                        }
                        return true;
                    }
                },
                messages: { //custom rules messages
                    productnamevalidation: function (input) {
                        // return the message text
                        return input.attr("data-val-productnamevalidation");
                    }
                }
            });
        })(jQuery, kendo);
     
        //show server errors if any
        function error_handler(e) {
            if (e.errors) {
                var message = "Errors:\n";
                $.each(e.errors, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function () {
                            message += this + "\n";
                        });
                    }
                });
                alert(message);
            }
        }
    </script>


    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  11. William Dunn
    William Dunn avatar
    15 posts
    Member since:
    May 2010

    Posted 12 Apr 2013 Link to this post

    Thanks Petur,

    Kendo's documentation had it right in front of me and I glossed right over it.

    messages: {
     email: function (input) {
    return input.attr("data-val-email");
    }
    }

  12. Daniel
    Daniel avatar
    192 posts
    Member since:
    Mar 2012

    Posted 12 Apr 2013 Link to this post

    that's the right way William,thanks for the ideea.
  13. Loren
    Loren avatar
    9 posts
    Member since:
    Dec 2013

    Posted 16 Jul 2014 Link to this post

    I know this is a bit old. But i just spend sometime trying to make this work and figured i would post it to help other in the future
    (function ($, kendo) {
        var dateValidation = function (input) {
            if (input.filter("[data-val-date]").length > 0) {
                return input.val() === "" || kendo.parseDate(input.val(), ["MMddyyyy", "MMddyy", "MM/dd/yyyy", "MM/dd/yy"]) !== null;
            }
            return true;
        };
     
        $.extend(true, kendo.ui.validator, {
            rules: {
                date: dateValidation,
                mvcdate: dateValidation
            },
            messages: {
                email: function (input) {
                    if (input.attr("data-val-email")) {
                        return input.attr("data-val-email");
                    }
                    else {
                        return "{0} is not valid email";
                    }
                }
            }
        });
    })(jQuery, kendo);
    . Below is a more Global way so you dont have to set that custom rule up each time you want to validate an email in your system. Also I put in a fall-back in case the [EmailAddress] DataAnnotation is missed.

  14. ajith
    ajith avatar
    21 posts
    Member since:
    Apr 2015

    Posted 05 May 2015 in reply to Petur Subev Link to this post

    not work for me. need to include any js files?

    plz help me

Back to Top
Kendo UI is VS 2017 Ready