RequiredIf Conditional Validation Attribute
Asked Answered
L

7

75

I was looking for some advice on the best way to go about implementing a validation attribute that does the following.

Model

public class MyInputModel 
{
    [Required]
    public int Id {get;set;}

    public string MyProperty1 {get;set;}
    public string MyProperty2 {get;set;}
    public bool MyProperty3 {get;set;}

}

I want to have atleast prop1 prop2 prop3 with a value and if prop3 is the only value filled it it should not equal false. How would i go about writing a validation attribute(s?) for this?

Thanks for any help!

Lyndell answered 12/9, 2011 at 16:18 Comment(3)
You may checkout the following blog post for a sample implementation of a [RequiredIf] custom validation attribute. It compares against a single other property value but you could easily tweak the IsValid method to meet your requirements.Headforemost
just a small not on the ExpressiveAnnotations. The package is not supported above .NET 4.8Shred
there is a fork of ExpressiveAnnotations for .NET Core (also on NuGet)Tracheitis
T
122

I had the same problem yesterday, but I did it in a very clean way which works for both client side and server side validation.

Condition: Based on the value of other property in the model, you want to make another property required. Here is the code:

public class RequiredIfAttribute : RequiredAttribute
{
  private String PropertyName { get; set; }
  private Object DesiredValue { get; set; }

  public RequiredIfAttribute(String propertyName, Object desiredvalue)
  {
    PropertyName = propertyName;
    DesiredValue = desiredvalue;
  }

  protected override ValidationResult IsValid(object value, ValidationContext context)
  {
    Object instance = context.ObjectInstance;
    Type type = instance.GetType();
    Object proprtyvalue = type.GetProperty(PropertyName).GetValue(instance, null);
    if (proprtyvalue.ToString() == DesiredValue.ToString())
    {
      ValidationResult result = base.IsValid(value, context);
      return result;
    }
    return ValidationResult.Success;
  }
}

PropertyName is the property on which you want to make your condition
DesiredValue is the particular value of the PropertyName (property) for which your other property has to be validated for required

Say you have the following:

public enum UserType
{
  Admin,
  Regular
}

public class User
{
  public UserType UserType {get;set;}

  [RequiredIf("UserType",UserType.Admin,
              ErrorMessageResourceName="PasswordRequired", 
              ErrorMessageResourceType = typeof(ResourceString))]
  public string Password { get; set; }
}

At last but not the least, register adapter for your attribute so that it can do client side validation (I put it in global.asax, Application_Start)

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredIfAttribute),
                                                      typeof(RequiredAttributeAdapter));

EDITED

Some people have reported issues that the client side fires no matter what or it does not work. So I modified the above code to do conditional client side validation with Javascript as well. For this case you don't need to register adapter

 public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
    {
        private String PropertyName { get; set; }
        private Object DesiredValue { get; set; }
        private readonly RequiredAttribute _innerAttribute;

        public RequiredIfAttribute(String propertyName, Object desiredvalue)
        {
            PropertyName = propertyName;
            DesiredValue = desiredvalue;
            _innerAttribute = new RequiredAttribute();
        }

        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            var dependentValue = context.ObjectInstance.GetType().GetProperty(PropertyName).GetValue(context.ObjectInstance, null);

            if (dependentValue.ToString() == DesiredValue.ToString())
            {
                if (!_innerAttribute.IsValid(value))
                {
                    return new ValidationResult(FormatErrorMessage(context.DisplayName), new[] { context.MemberName });
                }
            }
            return ValidationResult.Success;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessageString,
                ValidationType = "requiredif",
            };
            rule.ValidationParameters["dependentproperty"] = (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(PropertyName);
            rule.ValidationParameters["desiredvalue"] = DesiredValue is bool ? DesiredValue.ToString().ToLower() : DesiredValue;

            yield return rule;
        }
    }

And finally the javascript ( bundle it and renderit...put it in its own script file)

$.validator.unobtrusive.adapters.add('requiredif', ['dependentproperty', 'desiredvalue'], function (options) {
    options.rules['requiredif'] = options.params;
    options.messages['requiredif'] = options.message;
});

$.validator.addMethod('requiredif', function (value, element, parameters) {
    var desiredvalue = parameters.desiredvalue;
    desiredvalue = (desiredvalue == null ? '' : desiredvalue).toString();
    var controlType = $("input[id$='" + parameters.dependentproperty + "']").attr("type");
    var actualvalue = {}
    if (controlType == "checkbox" || controlType == "radio") {
        var control = $("input[id$='" + parameters.dependentproperty + "']:checked");
        actualvalue = control.val();
    } else {
        actualvalue = $("#" + parameters.dependentproperty).val();
    }
    if ($.trim(desiredvalue).toLowerCase() === $.trim(actualvalue).toLocaleLowerCase()) {
        var isValid = $.validator.methods.required.call(this, value, element, parameters);
        return isValid;
    }
    return true;
});

You need obviously the unobstrusive validate jQuery to be included as requirement

Tolson answered 12/4, 2013 at 15:57 Comment(23)
@Dan Hunex : In MVC4 I have not managed to work properly on client side and it fires up the validation no matter what the DesiredValue is. Any help pls?Childish
Can I see the attribute you createdTolson
Same issue as @H.Johnson. The client side validation fires no matter what. Any way to resolve this?Client
I am having the same issue. It fires no matter what.Arid
For those of you who find it not working client side, I have added a newer version with javascriptTolson
Interesting I did the edit withowt being logged in .. hmmm ..but ok I hope this will help some when searching for this same thing.Volney
Fantastic reply here, +1 from me, but could you explain the following ErrorMessageResourceType = typeof(ResourceStrings)) ResourceStrings is not recognised on my VS2013.Derm
Ignore this, I've fixed it! Thanks!Derm
works great. you can avoid including the above validate script and reuse the already available "Required" client-side validation by setting ErrorMessage = string.Format("{0} is required.", metadata.DisplayName), validationType = "required" in RequiredIfAttribute.csAtomicity
@Atomicity Thats Worked! ThanksKnotweed
how do I force radio button if textbox not empty [RequiredIf("SomeTextBox", "what to say here" , ErrorMessage = "Message")]Kandis
I'd like to point out that if you are concerned about Localization and need the standard RequiredAttribute validation messages, you can just simply set ErrorMessage = _innerAttribute.FormatErrorMessage(metadata.GetDisplayName()) in the GetClientValidationRulesConventional
So for me, this kept returning "This field is required" in the summary and I couldn't get the correct message to show. I think there is a mismatch by calling the existing required method, but setting the message on the new requiredif method. What I did was swap out the 2 lines inside the comparison if check (the one with the $.trim statements) and replaced it with the code inside the required method inside jquery.validate.js. Basically do the same thing the required method does, but inside the requiredif method so the correct message shows.Dinette
@DanHunex Could you please explain how to use the JS code you wrote? Could you give an example of where exactly to write it and how will it be used?Burny
@MishaZaslavsky As I stated , put it in its own file, bundle it and it will work automatically when you use the RequiredIf attribute in your model classes. How to bundle?! you may take a look at this tutorialsteacher.com/mvc/scriptbundle-mvcTolson
This is a great answer, thanks for saving me hours of working out the code itselfSchumer
@MikeUpjohn Could you tell me how did you fix ErrorMessageResourceType problem? Mine not working.Milldam
I had to add || "false" to make RequiredIf(checkbox) work: if (controlType == "checkbox" || controlType == "radio") { var control = $("input[id$='" + parameters.dependentproperty + "']:checked"); actualvalue = control.val() || "false";Shipshape
@Shipshape Yes. You can do that. I wrote this based on checkbox. If you even have other control type to depend, you can add more conditionsTolson
The type System.Web.Mvc.RequiredAttributeAdapter must have a public constructor which accepts three parameters of types System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext, and RequiredIfAttribute Parameter name: adapterTypeBetz
Correct. But how can I deal with the custom property with condition.I am not getting PropertyName from the context into the [dependentproperty].. Thats OK. I've change the code and directly assisgn PropertyName into [dependentproperty] but then in jquery the filed with the same name not available in html context. So actula value is still undefined,Nims
@DanHunex I have to implement this type of validation in ASP.NET MVC Core 3.1. Do you have any idea, how to implement?Ejection
@Ejection You should use a similar approach. The documentation here learn.microsoft.com/en-us/aspnet/core/mvc/models/… gives good example of custom validationTolson
B
35

I know the topic was asked some time ago, but recently I had faced similar issue and found yet another, but in my opinion a more complete solution. I decided to implement mechanism which provides conditional attributes to calculate validation results based on other properties values and relations between them, which are defined in logical expressions.

Using it you are able to achieve the result you asked about in the following manner:

[RequiredIf("MyProperty2 == null && MyProperty3 == false")]
public string MyProperty1 { get; set; }

[RequiredIf("MyProperty1 == null && MyProperty3 == false")]
public string MyProperty2 { get; set; }

[AssertThat("MyProperty1 != null || MyProperty2 != null || MyProperty3 == true")]
public bool MyProperty3 { get; set; }

More information about ExpressiveAnnotations library can be found here. It should simplify many declarative validation cases without the necessity of writing additional case-specific attributes or using imperative way of validation inside controllers.

Balikpapan answered 14/8, 2013 at 15:27 Comment(9)
Many, many thanks for this library. In terms of extensibility, it beats all the others.Assonance
I applied all the steps mentioned on github.com/JaroslawWaliszko/ExpressiveAnnotations but did not worked for me giving the following error: "The type System.Web.Mvc.RequiredAttributeAdapter must have a public constructor which accepts three parameters of types System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext, and ExpressiveAnnotations.Attributes.RequiredIfAttribute Parameter name: adapterType"Childish
@H.Johnson: This exception is unlikely to occur. I assume there is an issue in your code (maybe you have types ambiguity - are you sure that in your Global.asax you have registered adapters for attributes from appropriate namespace i.e. ExpressiveAnnotations.Attributes, and not any other ones?). To help you I need more information, above is to less. Finally take a look at sample project on github and try to investigate where is the difference.Balikpapan
@H.Johnson: ad 1) MvcUnobtrusiveValidatorProvider.dll should be added automatically to your references by NuGet. If for some reason it is not there - add reference manually. ad 2) For client-side validation to work, despite ExpressiveAnnotations.dll, mentioned MvcUnobtrusiveValidatorProvider.dll also needs to be there. What's more the expressive.annotations.validate.js should be included in bundle below jquery validation files, and added on specified page (see sample proj).Balikpapan
@JaroslawWaliszko: Thanks for your kind help. Actually your message encouraged me and I tried again. I do not know what the problem was, but by the last try I have managed to work it "on server and client side". That is exactly wonderful & simple and most flexible I have searched for many web sites. Thanks a lot for your nice example and help. I voted up ;)Childish
On the other hand, for my prior message I had managed to find ExpressiveAnnotations.MvcUnobtrusiveValidatorProvider.dll from the project I tried first. Thanks a lot again for your consideration.Childish
Just a head's-up, this requires bootstrap 3.0.1.1 or higher. I can't use it because we're using Bootstrap 3.0.0.Zared
Thank you so much for this... we should all up vote this. This solves those complex "required if" validations :)Conney
Is this library only for ASP.NET, and not ASP.NET Core?Tahsildar
G
14

I got it to work on ASP.NET MVC 5

I saw many people interested in and suffering from this code and i know it's really confusing and disrupting for the first time.

Notes

  • worked on MVC 5 on both server and client side :D
  • I didn't install "ExpressiveAnnotations" library at all.
  • I'm taking about the Original code from "@Dan Hunex", Find him above

Tips To Fix This Error

"The type System.Web.Mvc.RequiredAttributeAdapter must have a public constructor which accepts three parameters of types System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext, and ExpressiveAnnotations.Attributes.RequiredIfAttribute Parameter name: adapterType"

Tip #1: make sure that you're inheriting from 'ValidationAttribute' not from 'RequiredAttribute'

 public class RequiredIfAttribute : ValidationAttribute, IClientValidatable { ...}

Tip #2: OR remove this entire line from 'Global.asax', It is not needed at all in the newer version of the code (after edit by @Dan_Hunex), and yes this line was a must in the old version ...

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredIfAttribute), typeof(RequiredAttributeAdapter));

Tips To Get The Javascript Code Part Work

1- put the code in a new js file (ex:requiredIfValidator.js)

2- warp the code inside a $(document).ready(function(){........});

3- include our js file after including the JQuery validation libraries, So it look like this now :

@Scripts.Render("~/bundles/jqueryval")
<script src="~/Content/JS/requiredIfValidator.js"></script>

4- Edit the C# code

from

rule.ValidationParameters["dependentproperty"] = (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(PropertyName);

to

rule.ValidationParameters["dependentproperty"] = PropertyName;

and from

if (dependentValue.ToString() == DesiredValue.ToString())

to

if (dependentValue != null && dependentValue.ToString() == DesiredValue.ToString())

My Entire Code Up and Running

Global.asax

Nothing to add here, keep it clean

requiredIfValidator.js

create this file in ~/content or in ~/scripts folder

    $.validator.unobtrusive.adapters.add('requiredif', ['dependentproperty', 'desiredvalue'], function (options)
{
    options.rules['requiredif'] = options.params;
    options.messages['requiredif'] = options.message;
});


$(document).ready(function ()
{

    $.validator.addMethod('requiredif', function (value, element, parameters) {
        var desiredvalue = parameters.desiredvalue;
        desiredvalue = (desiredvalue == null ? '' : desiredvalue).toString();
        var controlType = $("input[id$='" + parameters.dependentproperty + "']").attr("type");
        var actualvalue = {}
        if (controlType == "checkbox" || controlType == "radio") {
            var control = $("input[id$='" + parameters.dependentproperty + "']:checked");
            actualvalue = control.val();
        } else {
            actualvalue = $("#" + parameters.dependentproperty).val();
        }
        if ($.trim(desiredvalue).toLowerCase() === $.trim(actualvalue).toLocaleLowerCase()) {
            var isValid = $.validator.methods.required.call(this, value, element, parameters);
            return isValid;
        }
        return true;
    });
});

_Layout.cshtml or the View

@Scripts.Render("~/bundles/jqueryval")
<script src="~/Content/JS/requiredIfValidator.js"></script>

RequiredIfAttribute.cs Class

create it some where in your project, For example in ~/models/customValidation/

    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Your_Project_Name.Models.CustomValidation
{
    public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
    {
        private String PropertyName { get; set; }
        private Object DesiredValue { get; set; }
        private readonly RequiredAttribute _innerAttribute;

        public RequiredIfAttribute(String propertyName, Object desiredvalue)
        {
            PropertyName = propertyName;
            DesiredValue = desiredvalue;
            _innerAttribute = new RequiredAttribute();
        }

        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            var dependentValue = context.ObjectInstance.GetType().GetProperty(PropertyName).GetValue(context.ObjectInstance, null);

            if (dependentValue != null && dependentValue.ToString() == DesiredValue.ToString())
            {
                if (!_innerAttribute.IsValid(value))
                {
                    return new ValidationResult(FormatErrorMessage(context.DisplayName), new[] { context.MemberName });
                }
            }
            return ValidationResult.Success;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessageString,
                ValidationType = "requiredif",
            };
            rule.ValidationParameters["dependentproperty"] = PropertyName;
            rule.ValidationParameters["desiredvalue"] = DesiredValue is bool ? DesiredValue.ToString().ToLower() : DesiredValue;

            yield return rule;
        }
    }
}

The Model

    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Your_Project_Name.Models.CustomValidation;

namespace Your_Project_Name.Models.ViewModels
{

    public class CreateOpenActivity
    {
        public Nullable<int> ORG_BY_CD { get; set; }

        [RequiredIf("ORG_BY_CD", "5", ErrorMessage = "Coordinator ID is required")] // This means: IF 'ORG_BY_CD' is equal 5 (for the example)  > make 'COR_CI_ID_NUM' required and apply its all validation / data annotations
        [RegularExpression("[0-9]+", ErrorMessage = "Enter Numbers Only")]
        [MaxLength(9, ErrorMessage = "Enter a valid ID Number")]
        [MinLength(9, ErrorMessage = "Enter a valid ID Number")]
        public string COR_CI_ID_NUM { get; set; }
    }
}

The View

Nothing to note here actually ...

    @model Your_Project_Name.Models.ViewModels.CreateOpenActivity
@{
    ViewBag.Title = "Testing";
}

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CreateOpenActivity</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.ORG_BY_CD, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ORG_BY_CD, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ORG_BY_CD, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.COR_CI_ID_NUM, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.COR_CI_ID_NUM, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.COR_CI_ID_NUM, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

I may upload a project sample for this later ...

Hope this was helpful

Thank You

Gatepost answered 1/5, 2017 at 14:36 Comment(4)
I had a problem with the client error messages coming back as "The field [object Object] is invalid." If you encounter the same, replace ErrorMessage = ErrorMessageString, with ErrorMessage = ErrorMessage ?? string.Format(ErrorMessageString, metadata.DisplayName),Trucker
@Adel Mourad I have to implement a similar kind of validation in ASP.NET MVC Core 3.1. Do you have any idea how to implement this code in asp.net MVC Core?Ejection
@Ejection You many switch the required validation on/off manually using C# and JS. For C# use the ModelState.Remove(PropName") to remove the validation from some property before calling ModelState.IsValid ....Gatepost
@AdelMourad thank you for this complete solution. I just have a remark or improvement : it will be better to replace dependentValue.ToString() == DesiredValue.ToString() by string.Equals(dependentValue.ToString(), DesiredValue.ToString(), StringComparison.OrdinalIgnoreCase) or add string comparaison mode as parameterRoybn
N
2

If you try to use "ModelState.Remove" or "ModelState["Prop"].Errors.Clear()" the "ModelState.IsValid" stil returns false.

Why not just removing the default "Required" Annotation from Model and make your custom validation before the "ModelState.IsValid" on Controller 'Post' action? Like this:

if (!String.IsNullOrEmpty(yourClass.Property1) && String.IsNullOrEmpty(yourClass.dependantProperty))            
            ModelState.AddModelError("dependantProperty", "It´s necessary to select some 'dependant'.");
Negron answered 25/8, 2016 at 12:39 Comment(1)
I found this solution the easiest. The error raised this way even shows together with errors raised by the built in [Required] attribute. Thanks!Include
T
2

Expanding on the notes from Adel Mourad and Dan Hunex, I amended the code to provide an example that only accepts values that do not match the given value.

I also found that I didn't need the JavaScript.

I added the following class to my Models folder:

public class RequiredIfNotAttribute : ValidationAttribute, IClientValidatable
{
    private String PropertyName { get; set; }
    private Object InvalidValue { get; set; }
    private readonly RequiredAttribute _innerAttribute;

    public RequiredIfNotAttribute(String propertyName, Object invalidValue)
    {
        PropertyName = propertyName;
        InvalidValue = invalidValue;
        _innerAttribute = new RequiredAttribute();
    }

    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        var dependentValue = context.ObjectInstance.GetType().GetProperty(PropertyName).GetValue(context.ObjectInstance, null);

        if (dependentValue.ToString() != InvalidValue.ToString())
        {
            if (!_innerAttribute.IsValid(value))
            {
                return new ValidationResult(FormatErrorMessage(context.DisplayName), new[] { context.MemberName });
            }
        }
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = ErrorMessageString,
            ValidationType = "requiredifnot",
        };
        rule.ValidationParameters["dependentproperty"] = (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(PropertyName);
        rule.ValidationParameters["invalidvalue"] = InvalidValue is bool ? InvalidValue.ToString().ToLower() : InvalidValue;

        yield return rule;
    }

I didn't need to make any changes to my view, but did make a change to the properties of my model:

    [RequiredIfNot("Id", 0, ErrorMessage = "Please select a Source")]
    public string TemplateGTSource { get; set; }

    public string TemplateGTMedium
    {
        get
        {
            return "Email";
        }
    }

    [RequiredIfNot("Id", 0, ErrorMessage = "Please enter a Campaign")]
    public string TemplateGTCampaign { get; set; }

    [RequiredIfNot("Id", 0, ErrorMessage = "Please enter a Term")]
    public string TemplateGTTerm { get; set; }

Hope this helps!

Thoreau answered 31/10, 2017 at 9:47 Comment(0)
C
1

The main difference from other solutions here is that this one reuses logic in RequiredAttribute on the server side, and uses required's validation method depends property on the client side:

public class RequiredIf : RequiredAttribute, IClientValidatable
{
    public string OtherProperty { get; private set; }
    public object OtherPropertyValue { get; private set; }

    public RequiredIf(string otherProperty, object otherPropertyValue)
    {
        OtherProperty = otherProperty;
        OtherPropertyValue = otherPropertyValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty);
        if (otherPropertyInfo == null)
        {
            return new ValidationResult($"Unknown property {OtherProperty}");
        }

        object otherValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
        if (Equals(OtherPropertyValue, otherValue)) // if other property has the configured value
            return base.IsValid(value, validationContext);

        return null;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
        rule.ValidationType = "requiredif"; // data-val-requiredif
        rule.ValidationParameters.Add("other", OtherProperty); // data-val-requiredif-other
        rule.ValidationParameters.Add("otherval", OtherPropertyValue); // data-val-requiredif-otherval

        yield return rule;
    }
}

$.validator.unobtrusive.adapters.add("requiredif", ["other", "otherval"], function (options) {
    var value = {
        depends: function () {
            var element = $(options.form).find(":input[name='" + options.params.other + "']")[0];
            return element && $(element).val() == options.params.otherval;
        }
    }
    options.rules["required"] = value;
    options.messages["required"] = options.message;
});
Craniometry answered 9/1, 2017 at 23:10 Comment(0)
V
0

I think using IValidatableObject is a good choice.

public class MyInputModel : IValidateObject
{
    [Required]
    public int Id {get;set;}

    public string MyProperty1 {get;set;}
    public string MyProperty2 {get;set;}
    public bool MyProperty3 {get;set;}

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (MyProperty1==null&&MyProperty2==null&&MyPropterty3!=false) //whatever condition
        {
        yield return new ValidationResult(
            "Custom complex error");
        }
    }

}
Volta answered 21/2, 2022 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.