2

I have a custom validation attribute:

public class RequireIfPropertyIsFalseAttribute : ValidationAttribute
    {
        private string basisProperty { get; set; }
        public RequireIfPropertyIsFalseAttribute(string basisProperty)
        {
            this.basisProperty = basisProperty;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var basisProp = validationContext.ObjectType.GetProperty(basisProperty);
            var isFalse = !(bool)basisProp.GetValue(validationContext.ObjectInstance, null);

            if (isFalse)
            {
                if (value == null || (value.GetType() == typeof(string) && string.IsNullOrEmpty(((string)value).Trim())))
                        return new ValidationResult(this.ErrorMessage);
            }

            return ValidationResult.Success;
        }
    }

I use it for both Model properties:

public bool NoAgeProvided { get; set; }
[RequireIfPropertyIsFalse(nameof(NoAgeProvided), ErrorMessage = "This is required")]
[Display(Name = "Age")]
public int Age { get; set; }

public bool NoNameProvided { get; set; }
[RequireIfPropertyIsFalse(nameof(NoNameProvided), ErrorMessage = "This is required")]
[Display(Name = "Name")]
public string Name { get; set; }

Upon validation, the Name validation message shows "This is required". However, for the Age property, "The Age field is required" is displaying on the validation message. What am I doing wrong? How can I display the set ErrorMessage?

1 Answer 1

1

Because property Age is typeof int, it is always required (an int cannot be null) and if you leave the textbox empty, a null value is submitted and the Required validation is executed first and that error message is displayed.

Change you property to be nullable

public int? Age { get; set; }

Note that it works for your Name property because its string which is nullable by default.

As a side note, you should consider implementing IClientValidatable so that you can get client side validation as well. Refer The Complete Guide To Validation In ASP.NET MVC 3 - Part 2.

Sign up to request clarification or add additional context in comments.

4 Comments

I suspected that that is the case. However, making the field nullable would also mean I have to change the associated database table column's is null attribute. I'll read the link that you provided. Thank you so much!
Your editing data so you ALWAYS use a view model, not data models in a view.
In any case, what would you expect to store in that db column if its not provided (a value of 0 would not make sense)
Tbh the client's requirements are murky right now, I have a non nullable table column and a nullable model property. I was hoping I could find a way to make the validation for the contrasting nature of the two fields. The requirements may change in the future, but for now I am working for a solution. I am still reading the link that you provided, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.