Combine custom validation attribute with formatting
<p>I have a metadata class for my <code>Customer</code> where I validate the <code>PurchaseDate</code>.</p>

<ul>
<li>The first annotation (<code>DataType</code>) is for formatting the date in an <code>EditorFor</code>, to show just the date part.</li>
<li>The second annotation is a custom validation to verify that the value is a DateTime, including a custom error message.</li>
</ul>

<p>My problem is that the first annotation will cancel out the errormessage of the second annotation.</p>

<p>Is it possible to combine these two using only data annotations? Or do I have to format the date in the <code>EditorFor</code>?</p>

<pre><code>[MetadataType(typeof(Customer_Metadata))]
public partial class Customer { }

public class Customer_Metadata
{
    [DataType(DataType.Date)]
    [MyDate(ErrorMessage = "Invalid purchase date")]
    public DateTime? PurchaseDate { get; set; }
}
</code></pre>

<p>The same problem occurs if I try to replace the <code>[DataType(DataType.Date)]</code>
with</p>

<pre><code>[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
</code></pre>

<p>I won't get my custom error message.</p>

<p><strong>EDIT</strong><br>
My main goal is to have a custom error message while also only showing the date part in the rendered input field. Is it possible with only data annotations?</p>

<p>Here's the <code>MyDate</code> attribute:</p>

<pre><code>public class MyDate : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        DateTime dt;
        var test = DateTime.TryParse((value ?? string.Empty).ToString(), out dt);
        if (test)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(ErrorMessage);
        }
    }
}
</code></pre>