0

I need implement a cusotm attribute then using asp.net data annotation to validate a class. Unfortunately, the attribute class is not called at run time. Please help me out. Many thanks. Below is the source code.

using System;
using System.Collections.Generic;
using CaseMgr.Model.Base;

using System.Linq;
using System.ComponentModel.DataAnnotations;

namespace CaseMgr.Model.BusinessObjects
{
    public partial class PatLiverException : BusinessBase<decimal>, IComparable<PatLiverException>, IEquatable<PatLiverException>
    {
        private LiverExcepDisease _liverExcepDisease = null;
        private DateTime _sccApprovalDate = new DateTime();

        public PatLiverException() { }

        public virtual LiverExcepDisease LiverExcepDisease
        {
            get { return _liverExcepDisease; }
            set { _liverExcepDisease = value; }
        }

        [SccApprovalDateValidate("SccApprovalDate", "LiverExcepDisease")]
        public virtual DateTime SccApprovalDate
        {
            get { return _sccApprovalDate; }
            set { _sccApprovalDate = value; }
        }
    }

    public class SccApprovalDateValidateAttribute : ValidationAttribute
    {
        public string m_SccApprovalDate { get; private set; }
        public string m_LiverExcepDisease { get; private set; }

        public SccApprovalDateValidateAttribute(string SccApprovalDate_PropertyName, string LiverExcepDisease_PropertyName)
        {
            this.m_SccApprovalDate = SccApprovalDate_PropertyName;
            this.m_LiverExcepDisease = LiverExcepDisease_PropertyName;
        }

        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            var SccApprovalDate_Property = context.ObjectType.GetProperty(m_SccApprovalDate);
            DateTime SccApprovalDate_Value = (DateTime)SccApprovalDate_Property.GetValue(context.ObjectInstance, null);

            var LiverExcepDisease_Property = context.ObjectType.GetProperty(m_LiverExcepDisease);
            LiverExcepDisease LiverExcepDisease_Value = (LiverExcepDisease)LiverExcepDisease_Property.GetValue(context.ObjectInstance, null);

            if (SccApprovalDate_Value != null && SccApprovalDate_Value != DateTime.MinValue && SccApprovalDate_Value != DateTime.MaxValue)
            {
                return LiverExcepDisease_Value.Id == 10 ? ValidationResult.Success : new ValidationResult("When other, SccApprovalDate can not be null.");
            }
            else
            {
                return ValidationResult.Success;
            }
        }
    }
}
Deep in Development
  • 497
  • 2
  • 8
  • 24
  • 2
    Post code as text, not as an image. We don't program with images. – mason Nov 20 '15 at 15:40
  • Hi mason, I just re-post code as text now. please check it. tks. – Deep in Development Nov 20 '15 at 16:26
  • Not related, but its completely unnecessary to add the parameter `string SccApprovalDate_PropertyName` because its the property your applying it to (the `object value` parameter in the `IsValid()` method is the value of your property. –  Nov 21 '15 at 06:20

3 Answers3

0

If you are going to use the validation attribute on a class you need:

[AttributeUsage(AttributeTargets.Class)]
public class Sccxxxxxxxxx : ValidationAttribute

https://msdn.microsoft.com/en-us/library/tw5zxet9.aspx?f=255&MSPPError=-2147217396

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
0

Take a look at this post: ASP.NET MVC: Custom Validation by DataAnnotation

Also, if you put a breakpoint inside SccApprovalDateValidateAttribute.ValidationResult() does it get hit?

Community
  • 1
  • 1
univ
  • 717
  • 4
  • 12
0

Your code looks okay, except I cannot see where you call or use the property,SccApprovalDate, because your validation attribute declared on the property will only be invoked if the property is called or used by the run-time. Can you post how you use the property SccApprovalDate?

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27