8

This question has to have been asked before, but I think the search terms are too generic for me to find the answer I'm looking for, so I'll ask it again.

I have a model with an int property, and a range annotation.

If the user enters something other than an int, the validation message responds with The value '<bad data>' is not valid for '<property name>'... which is great, but I want to provide a bit more feedback, i.e., Expecting an integer value in this field..

Since this validation fails before the other validators take a look, I don't know how (or if it's possible) to override the default validator message for this.

What are my options?

per request, I am posting the code, but there's not a lot to it:

[Range(0,65535, ErrorMessage="Port must be between 0 and 65535.")] 
public int Port { get; set; }

There is validation that occurs before it reaches the RangeAttribute. I want to replace the default message with one of my own choosing.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254

3 Answers3

5

If you're using standard annotations, you should be able to override the error message with something like this:

[MyAnnotation(...., ErrorMessage = "My error message")]
public int myInt { get; set; }

Or do you actually want to append to the default error message instead of replacing it (not clear in question)?

Update: Misread -- suggest this as the answer: How to change the ErrorMessage for int model validation in ASP.NET MVC? or better yet How to change 'data-val-number' message validation in MVC while it is generated by @Html helper

Community
  • 1
  • 1
Cymen
  • 14,079
  • 4
  • 52
  • 72
  • Ah! Misread -- I think you want this then: http://stackoverflow.com/questions/6587816/how-to-change-the-errormessage-for-int-model-validation-in-asp-net-mvc – Cymen Sep 08 '11 at 16:57
  • 1
    This is another option -- it actually has an accepted answer too: http://stackoverflow.com/questions/4828297/how-to-change-data-val-number-message-validation-in-mvc-while-it-generate-by-he/6405298#6405298 – Cymen Sep 08 '11 at 17:01
  • Yeah that last one seemed to provide the answer I was looking for... but in such a way that I've decided I'll keep the default message. Way too much work for something that should be simple. – Jeremy Holovacs Sep 08 '11 at 17:09
  • You've probably caught it already but you can use `[Display(Name = "My nice name")]` in the case that your variable name doesn't look/read right in the error message. I am surprised too that it is that hard to change. – Cymen Sep 08 '11 at 17:11
4

You can also inherit IValidatableObject in your model class. You can write down your required logic in the Validate method. Please find sample code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    public class Alok : IValidatableObject
    {
        [Display(Name = "Property1")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Property1 is required.")]
        public int Property1 { get; set; }

        [Display(Name = "Property2")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Property2 is required.")]
        public int Property2 { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Property1 < Property2)
            {
                yield return new ValidationResult("Property 1 can't be less than Property 2.");
            }
        }
    }
}
alok_dida
  • 1,723
  • 2
  • 17
  • 36
  • 1
    I don't think this will work. The validation error is added to the model at mapping time which occurs before the "official" validation. – Jeremy Holovacs Sep 09 '11 at 12:38
1

read this question. In the link that the OP suggest you will find the way to replace the deafult error string that use the framework, while in the answer you will find a linnk to the other resources in case you want to change all of them. Look also here. Hope it helps

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Yeah updating the resource file is about furthest from my mind; additionally, it does not seem to support branching due to different type conversion errors. – Jeremy Holovacs Sep 08 '11 at 17:08
  • Guess you have to write your own extension like the answer you already find. Sorry – Iridio Sep 08 '11 at 17:10