4

I'm using ASP.NET MVC 3 with Fluent Validation. I'd like all my error messages to be worded and formatted the same, whether they are validation error messages or model binding error messages.

Let's say I have the following view-model:

[Validator(typeof(PersonValidator))]
public class Person
{
    [ScaffoldColumn(false)] public int    Id   { get; set; }
                            public string Name { get; set; }
                            public int    Age  { get; set; }
}

To validate this using Fluent Validation, I might use something like this:

public class EditorValidator : AbstractValidator<EditorModel>
{
    public EditorValidator()
    {
        RuleFor(model => model.Month.Value).InclusiveBetween(0, 120)
    }
}

If the user enters "abc" for Age, this causes a model binding error, not a validation error. This is because "abc" is not an int. The system never even gets to the point of asking whether "abc" is between 0 and 120, because "abc" cannot be stored in Age.

This is fine and makes sense. The problem is that the resulting error message is:

The field Age must be a number.

I'd like the message to be formatted and worded like other error message produced by Fluent Validation. In this case, I would like:

'Age' must be a number.

I realize this is only a subtle difference, but I'd like to have control of model binding error messages.

How can I customize the model binding error messages to match the error messages used by Fluent Validation?

devuxer
  • 41,681
  • 47
  • 180
  • 292

2 Answers2

4

I'm not sure if there's another way to do this, but I use Data Annotations Extensions, also available via NuGet (Install-Package DataAnnotationsExtensions.MVC3) for this exact type of thing. This package will give you an IntegerAttribute, and from there you can specify an error message like so:

[Integer(ErrorMessage = "'Age' must be a number.")]
public int Age { get; set; }
gram
  • 2,772
  • 20
  • 24
  • +1 for providing a viable solution, but I was hoping for something more centralized so I could just say that all errors involving `int` properties should receive the same error, e.g., `string.Format("'{0}' must be a number.", fieldName)`. – devuxer Jun 27 '11 at 03:44
  • Does this actually works? I just checked the source code of the data annotations extensions project and grabbed a copy of the date attribute and applied it, but for me that still doesn't work? – Maarten Kieft Dec 12 '16 at 15:45
  • I just found out its because I disabled client side validation. So you are still setting a validaiton error message, not a binding error message. – Maarten Kieft Dec 12 '16 at 16:00
0

Take a look at my answer here:

How to change 'data-val-number' message validation in MVC while it is generated by @Html helper

Actually, it's a very common question that you asked, so you should've googled the stackoverflow prior to posting.

Community
  • 1
  • 1
Denis Valeev
  • 5,975
  • 35
  • 41
  • I somehow didn't come across anything except one question dealing with localization that didn't quite answer my question. The question you linked to certainly wouldn't have come up in any of my searches. – devuxer Jun 27 '11 at 21:21