Neir0
Neir0

Reputation: 13367

Custom validation message

I have a problem with validating some data types. There are int, short, DateTime and so on except string. Suppose i have following property in my view model class

public int? LineNumber { get; set; }

When i input incorrect value i get mvc error "The value 'balblabl' is not valid for LineNumber."

But what if i want just out something like "Value incorrect"? Or what if i want to use other language? I have no idea how to do it(of course i can use string instead of int but it is painfull workaround)

i already tried dataannotationsextensions [DataAnnotationsExtensions.Integer(ErrorMessage = "Please enter a valid number.")] attribute. It is not working. I cannt to use custom validation attribute because of after binder convertation i get null value in all cases with incorrect value. I just cannt to do my own validation. I can to write my own binder but it looks like a joke. Really i think custom validation message is one of must have featerus and i cannt belive asp.net mvc doesnt has a simple way to do it.

Upvotes: 1

Views: 404

Answers (2)

Syneryx
Syneryx

Reputation: 1288

I would like to add another, in my opinion, easy way to add and maintain custom error messages. using the FluentValidation NuGet package.
It hooks up with mvc pretty easy have a look here

You can easily specify numerous rules for your models and display custom error messages that can use a resource file an example:

public class CreateProductCommandValidator : AbstractValidator<CreateProductCommand>
    {
        public CreateAgendaPointCommandValidator()
        {
            RuleFor(cmd => cmd.price)
                .NotEmpty()
                .WithMessage(Translations.CreateProduct_Price)

        }
    }
}

Documentation: FluentValidationDocumentation

Upvotes: 1

Daniel J.G.
Daniel J.G.

Reputation: 34992

Those errors are automatically added by the default model binder when it cannot parse the input string into its destination type. They are not like data annotations, where you can specify a resource instead of a harcoded string error message.

However you can use your own resource files, see this other question. Once you have created the resource file, you will update the global.asax for the default model binder to use it:

DefaultModelBinder.ResourceClassKey = "MyResources";

After a custom resource file has been set in the property ResourceClassKey of the default model binder, values will be resolved according to the following criteria (as per the MSDN):

  1. If the property is not set, MVC uses the standard MVC resources.

  2. If the property is set to an invalid class key (such as a resource file that does not exist), MVC throws an exception.

  3. If the property is set and the class key exists but the resource names in the file do not exist in the application, MVC uses the standard MVC resources.

  4. If the property is set and the specified resources are available, MVC uses the resources in the file.

If you need to know the key values for a particular message check this. For the message The value '{0}' is not valid for {1}., you will need to add a value with the key DefaultModelBinder_ValueInvalid

Upvotes: 0

Related Questions