1

I have a field in my model that looks like this

[Display(Name = "CompanyType", ResourceType = typeof(Captions), AutoGenerateFilter = true)]
[Lookup(LookupTextField = "TypeForDisplay", LookupEntity = "Type", LookupColumns = new[] { "TypeForDisplay" }, LookupType = LookupType.DropDown)]
public Guid? TypeID{ get; set; }

I want to add Required to that property, but not in all casses. It depends on the value of another property in the model. What is the best(easiest) way to do this?

user1892380
  • 33
  • 1
  • 7
  • [See the search results for `RequiredIf` validation attribute](http://stackoverflow.com/search?tab=votes&q=RequiredIf) – Shyju Aug 01 '16 at 13:41

3 Answers3

0

You can create a custom validator to accomplish this.

Here is a tutorial on how to go about it.

Bhushan Shah
  • 1,028
  • 8
  • 20
0
https://foolproof.codeplex.com/

Download the package using above link .Add a reference to the included .dll file

use on the top of property like

[RequiredIf]
[RequiredIfNot]
MMM
  • 3,132
  • 3
  • 20
  • 32
0

Implement IValidatableObject on your Model. You can then implement the Validate method to do an sort of custom validation that your need.

Here's an example http://weblogs.asp.net/scottgu/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3

Here's also a link to a RequiredIf implementation that has both server and client side validation. RequiredIf Conditional Validation Attribute

Community
  • 1
  • 1
Fran
  • 6,440
  • 1
  • 23
  • 35