views:

178

answers:

1

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

A: 

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.

griegs
@griegs Check out the DateTime.TryParse() method. http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
Ryan
@griegs - SelfValidation is a way to apply validation at the type level. Also, is it possible to do rulesets with DataAnnotations?
DotnetDude