views:

72

answers:

1

Hello everyone,

I've tried to override error message when input incorrect data type in input field on HTML form. For example I have the model like this.

public class Person
{
    public string FirstName {get;set;}
    public int Age {get;set;}
}

For view, I put text input for Age to get it value.

When type some string in Age text box like 'test' and press submit button. I got this error message

The value 'xxx' is not valid for Age

However, I want to change this message and try many way. There 's nothing effect this message value.

Please could you help me to solve this problem.

+1  A: 

You can use DataAnnotations to override the default error messages in MVC, as well as provide your own validation for whatever fields you need to. See the following:

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs

If you are using EF, you will need to create a MetaData class off the EF generated class and then add the data annotations. The end of the 2nd article covers how to write these classes for entity framework.

In your specific case you will want to do something like:

using System.ComponentModel.DataAnnotations;

public class Person
{
   public string FirstName {get;set;}

   [Range(0, 110, ErrorMessage = "<your error message>")]
   public int Age {get;set;}
}

UPDATE I did forget one thing that is easy to overlook, you need to include the following JS files for the data annotations to be picked up client side without having to do a post:

  • MicrosoftAjax.js

  • MicrosfotMvcValidation.js

These should be stock in the Scripts folder of your project (if you are using MVC 2), and you need to include them either on your page or in your master page,

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript"></script>

you will also need to include

<% Html.EnableClientValidation(); %>

on the page in question.

The client side validation will not let you submit the form until all fields meet validation requirements, effectively eliminating the need to check Model.IsValid in the HttpPost of your action in the controller.

Scott Lance
Thank you very much for your help. Scott Lance. However, it quite doesn't solve my problem since error occurs in type converter I think.
embarus
Is your view strongly typed? If the view is strongly typed, then the validation enforces that a value of type int is entered, otherwise you will receive the error message.
Scott Lance
Yes, it is strongly type view model. I just want to override the error message when binder cannot covert value to int. For example,I fill value 'test' for Age text box. After posting form, there should be error message "Please input number only." not The The value 'test' is not valid for Age.
embarus
If you are not using a strongly typed view for whatever reason you can see if the Html.ValidationMessage helper works for you. See: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.aspx
Scott Lance
Right, the data annotation should take care of that if the value in the input cannot be coerced to the type of the field (in this case int). So if your if everything is hooked up right, you should see the error message that you put in your data annotation when you type 'test' in the age box.
Scott Lance
there is also a "Numeric" data annotation
dave thieben
If you want the 'easy' way out you can always do the following in your view: <%: Html.TextBoxFor(m => Model.Age) %> <%: Html.ValidationMessageFor(m => Model.Age, "Please input number only")%>
Scott Lance
Thank you very much Scott Lance. This solves my problem with simple way. :)
embarus