attribute dependent on another field

后端 未结 5 1556
抹茶落季
抹茶落季 2020-11-27 04:03

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 bo         


        
5条回答
  •  清酒与你
    2020-11-27 04:36

    I have not seen anything out of the box that would allow you to do this.

    I've created a class for you to use, it's a bit rough and definitely not flexible.. but I think it may solve your current problem. Or at least put you on the right track.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.Globalization;
    
    namespace System.ComponentModel.DataAnnotations
    {
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
        public sealed class RequiredIfAttribute : ValidationAttribute
        {
            private const string _defaultErrorMessage = "'{0}' is required";
            private readonly object _typeId = new object();
    
            private string  _requiredProperty;
            private string  _targetProperty;
            private bool    _targetPropertyCondition;
    
            public RequiredIfAttribute(string requiredProperty, string targetProperty, bool targetPropertyCondition)
                : base(_defaultErrorMessage)
            {
                this._requiredProperty          = requiredProperty;
                this._targetProperty            = targetProperty;
                this._targetPropertyCondition   = targetPropertyCondition;
            }
    
            public override object TypeId
            {
                get
                {
                    return _typeId;
                }
            }
    
            public override string FormatErrorMessage(string name)
            {
                return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, _requiredProperty, _targetProperty, _targetPropertyCondition);
            }
    
            public override bool IsValid(object value)
            {
                bool result             = false;
                bool propertyRequired   = false; // Flag to check if the required property is required.
    
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
                string requiredPropertyValue            = (string) properties.Find(_requiredProperty, true).GetValue(value);
                bool targetPropertyValue                = (bool) properties.Find(_targetProperty, true).GetValue(value);
    
                if (targetPropertyValue == _targetPropertyCondition)
                {
                    propertyRequired = true;
                }
    
                if (propertyRequired)
                {
                    //check the required property value is not null
                    if (requiredPropertyValue != null)
                    {
                        result = true;
                    }
                }
                else
                {
                    //property is not required
                    result = true;
                }
    
                return result;
            }
        }
    }
    

    Above your Model class, you should just need to add:

    [RequiredIf("retirementAge", "retired", true)]
    public class MyModel
    

    In your View

    <%= Html.ValidationSummary() %> 
    

    Should show the error message whenever the retired property is true and the required property is empty.

    Hope this helps.

提交回复
热议问题