asp.net-mvcvalidationenterprise-librarydata-annotations

SelfValidation in DataAnnotations?


With Validation Application block, there's the following functionality:

  1. Creating Custom attributes
  2. Creating SelfValidation on the type
  3. Ability to read from external config file

I plan to use the DataAnnotations to replace the Validation application block. Are the above possible with DataAnnotations? If so, how'd I implement them?

Any help is appreciated


Solution

  • yes they are possible.

    Creating a very simple custom attribute;

    public class IsApplicantOldEnoughAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null) return false;
            DateTime enteredDate;
    
            try
            {
                enteredDate = (DateTime)value;
            }
            catch
            {
                return false;
            }
            if ((DateTime.Today.Year - enteredDate.Year) >= 14)
                return true;
            else
                return false;
        }
    }
    

    Reading from a config file is the same as any code that reads from a config file.

    Unsure what you mean by self validation though. Could you please fill me in and I'll try to give an example.