1

I have the following two properties in my model:

        [Display(ResourceType = typeof(WideFormatStrings), Name = "labelDynamicWidth")]
        public int? SelectedDynamicWidth { get; set; }

        [Display(ResourceType = typeof(WideFormatStrings), Name = "labelDynamicHeight")]
        public int? SelectedDynamicHeight { get; set; }

How can i make a validation attribute that will check that at least one of them is less then some number .. say 100 (The number is a variable from the DB).

Mortalus
  • 10,574
  • 11
  • 67
  • 117

2 Answers2

2

ExpressiveAnnotations has support for this. Here is the documentation. Something like this would accomplish what you want:

[AssertThat("SelectedDynamicWidth + SelectedDynamicHeight < 100")]
public int SelectedDynamicWidth { get; set; }

If the 100 is coming from the DB, youll most likely have to grab the value from the DB and put in a property in the view model:

[AssertThat("SelectedDynamicWidth + SelectedDynamicHeight < MaxSelectedDynamicValue")]
public int SelectedDynamicWidth { get; set; }

public int MaxSelectedDynamicValue { get; set; }
Brad C
  • 2,868
  • 22
  • 33
0

There is a thread about the different options to do conditional validation:ASP.NET MVC Conditional validation.

I think that a self validating model (implementing IValidatableObject) might solve your problem. You can find and example in the next entry How do I use IValidatableObject?

Community
  • 1
  • 1
jobmo
  • 835
  • 1
  • 9
  • 19