attribute dependent on another field

Viewed 44545

In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked.

Something like

public bool retired {get, set};

[RequiredIf("retired",true)]
public string retirementAge {get, set};

How can I do that?

Thank you.

5 Answers

Using NuGet Package Manager I intstalled this: https://github.com/jwaliszko/ExpressiveAnnotations

And this is my Model:

using ExpressiveAnnotations.Attributes;

public bool HasReferenceToNotIncludedFile { get; set; }

[RequiredIf("HasReferenceToNotIncludedFile == true", ErrorMessage = "RelevantAuditOpinionNumbers are required.")]
public string RelevantAuditOpinionNumbers { get; set; }

I guarantee you this will work!

Try my custom validation attribute:

[ConditionalRequired("retired==true")]
public string retirementAge {get, set};

It supports multiple conditions.

Related