How to create Custom Data Annotation Validators
Asked Answered
J

3

64

Wanting to create custom data annotation validation. Are there any useful guides / samples on how to create them?

Firstly:
StringLength with minimum and maximum length. I'm aware .NET 4 can do this, but want to do the same in .NET 3.5, if possible being able to define minimum length only (at least x chars), maximum length only (up to x chars), or both (between x and y chars).

Secondly:
Validation using modulus arithmetic - if the number is a valid length, I wish to validate using the Modulus 11 algorithm (I have already implemented it in JavaScript, so I guess it would just be a simple porting?)

Update:
Solved second problem, was just a case of copying over the JavaScript implementation and making a few tweaks, so don't need a solution for that.

Jena answered 5/8, 2010 at 10:13 Comment(0)
W
121

To create a custom data annotation validator follow these gudelines:

  1. Your class has to inherit from System.ComponentModel.DataAnnotations.ValidationAttribute class.
  2. Override bool IsValid(object value) method and implement validation logic inside it.

That's it.

IMPORTANT Caution

Sometimes developers check that value is not null/empty and return false. This is usually incorrect behaviour, because that's on Required validator to check which means that your custom validators should only validate non-null data but return true otherwise (see example). This will make them usable on mandatory (required) and non-mandatory fields.

Example

public class StringLengthRangeAttribute : ValidationAttribute
{
    public int Minimum { get; set; }
    public int Maximum { get; set; }

    public StringLengthRangeAttribute()
    {
        this.Minimum = 0;
        this.Maximum = int.MaxValue;
    }

    public override bool IsValid(object value)
    {
        string strValue = value as string;
        if (!string.IsNullOrEmpty(strValue))
        {
            int len = strValue.Length;
            return len >= this.Minimum && len <= this.Maximum;
        }
        return true;
    }
}

All properties can be set in attribute as you wish to set them.
Some examples:

[Required]
[StringLengthRange(Minimum = 10, ErrorMessage = "Must be >10 characters.")]

[StringLengthRange(Maximum = 20)]

[Required]
[StringLengthRange(Minimum = 10, Maximum = 20)]

When a particular property isn't set, its value is set in the constructor, so it always has a value. In above usage examples I deliberately added the Required validator as well, so it's in sync with the above caution I've written.

Important

So this validator will still work on your model value that's not required, but when it's present it validates (think of a text field in a web form, that's not required, but if a user enters a value in, it has to be valid).

Wait answered 5/8, 2010 at 10:39 Comment(7)
so, in case of null value should isValid return true?Raggletaggle
So how do you add extra options, e.g. [MyValidator(Min=1, Max=20)], Where both Min/Max (or just one) are optional?Jena
@onof: Yes. In that case it should return true.Wait
@Sam: I edited my answer and included an example of exactly what you're trying to do in the first scenario.Wait
@Rap it works for me in MVC 5. msdn.microsoft.com/en-us/library/cc679289(v=vs.110).aspxWeigand
modelstate is always valid [Required] [StringLengthRange(Minimum = 3, ErrorMessage = "Must be >3 characters.")] public string BookName { get; set; }Forcier
How can i register this ValidationAttribute in global.aspx? Im trying to use this code: DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(NumericAttribute), typeof(???));Zuber
B
14

Use the CustomValidationAttribute together with a validate function with signature

public static ValidationResult Validate(MyType x, ValidationContext context)

Example (for a string property)

using System.ComponentModel.DataAnnotations;

public class MyClass
{
    [CustomValidation(typeof(MyClass), "Validate")]
    public string MyProperty { get; set; }

    public static ValidationResult Validate(string x, ValidationContext context)
    {
        return (x == "valid")
            ? new ValidationResult(null)
            : ValidationResult.Success;
    }
}
Brassard answered 13/7, 2017 at 9:7 Comment(2)
What is the ValidationContext context argument used for?Thaxter
@petryuno1 You could use it to get the model if you happen to need to look at other properties in your vlaidation. e.g., var model = (MyClass)context.ObjectInstance;Duly
P
2

I know this is a really old topic but I had trouble finding the answer I actually wanted until I found this answer.

To summarise it, you need to configure the service on startup, creating appropriate objects that handle the error you want to return:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).ConfigureApiBehaviorOptions(options =>
        {
            options.InvalidModelStateResponseFactory = (context) =>
            {
                var errors = context.ModelState.Values.SelectMany(x => x.Errors.Select(p => new ErrorModel()
               {
                   ErrorCode = ((int)HttpStatusCode.BadRequest).ToString(CultureInfo.CurrentCulture),
                    ErrorMessage = p.ErrorMessage,
                    ServerErrorMessage = string.Empty
                })).ToList();
                var result = new BaseResponse
                {
                    Error = errors,
                    ResponseCode = (int)HttpStatusCode.BadRequest,
                    ResponseMessage = ResponseMessageConstants.VALIDATIONFAIL,

                };
                return new BadRequestObjectResult(result);
            };
       });
Pieter answered 27/9, 2021 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.