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

modify standard validation messages

12 Answers 987 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 21 Mar 2013, 11:16 AM
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

12 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 22 Mar 2013, 04:17 PM
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!
0
Daniel
Top achievements
Rank 1
answered on 25 Mar 2013, 08:35 AM
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
0
Alexander Valchev
Telerik team
answered on 27 Mar 2013, 08:17 AM
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!
0
Daniel
Top achievements
Rank 1
answered on 04 Apr 2013, 06:30 AM
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
0
Alexander Valchev
Telerik team
answered on 08 Apr 2013, 07:33 AM
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!
0
Daniel
Top achievements
Rank 1
answered on 10 Apr 2013, 08:12 AM
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,
0
William Dunn
Top achievements
Rank 1
answered on 10 Apr 2013, 03:46 PM
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


0
Petur Subev
Telerik team
answered on 12 Apr 2013, 12:05 PM
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!
0
William Dunn
Top achievements
Rank 1
answered on 12 Apr 2013, 12:33 PM
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");
}
}

0
Daniel
Top achievements
Rank 1
answered on 12 Apr 2013, 01:18 PM
that's the right way William,thanks for the ideea.
0
Loren
Top achievements
Rank 1
answered on 16 Jul 2014, 02:48 PM
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.

0
ajith
Top achievements
Rank 1
answered on 05 May 2015, 07:35 AM

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

plz help me

Tags
Validation
Asked by
Daniel
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Daniel
Top achievements
Rank 1
William Dunn
Top achievements
Rank 1
Petur Subev
Telerik team
Loren
Top achievements
Rank 1
ajith
Top achievements
Rank 1
Share this question
or