简体   繁体   中英

Custom Validation Attribute is not called ASP.NET MVC

Hello everyone I have create custom validation attribute and assign it to class level validation. Unfortunately, it is not called. I try every way that it think it could be solve the problem. However, it take me for hours and I can't find the attribute is not called by validation mechanism.

For illustrate you I put the following code.

Attribute

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class BooleanDependencyAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "กรุณากรอก{0}";
        private readonly object _typeId = new object();

        public string DependencyPropertyName { get; private set; }
        public string DependentPropertyName { get; private set; }

        public BooleanDependencyAttribute(string dependencyPropertyName, string dependentPropertyName)
            : base(_defaultErrorMessage)
        {
            DependencyPropertyName = dependencyPropertyName;
            DependentPropertyName = dependentPropertyName;
        }



        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,name);
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            bool dependencyValue = (bool) properties.Find(DependencyPropertyName, true /* ignoreCase */).GetValue(value);
            object dependentValue = properties.Find(DependentPropertyName, true /* ignoreCase */).GetValue(value);
            if (dependencyValue)
            {
                return true;
            }
            else
            {
                if (dependentValue == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

        }
    }

ViewModel

[BooleanDependency("ReleaseNow","ReleaseDate",ErrorMessage="Please enter release date")]
    public class ContentCreate
    {

        public string Title { get; set; }

        public DateTime? ReleaseDate { get; set; }


        public string Details { get; set; }

        public string Abstract { get; set; }

        public string Tags { get; set; }

        public bool ReleaseNow { get; set; }

    }

Please could you help me to solve this problem.

I found the solution. In fact validation in class level is called after all property-level validations are valid. Therefore I need to complete other required property then BooleanDependencyAttribute will called and valid value.

Thanks for view, edit the title and tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM