Look at almost any question about [DataType(DataType.EmailAddress)]
and validation and you'll find someone stating that the attribute is not for validation but to allow rendering of the property as a mailto link with DisplayFor or as an HTML 5 "email" input type with EditorFor. The only problem is the attribute does cause validation. In my view model I have the property:
[DataType(DataType.EmailAddress, ErrorMessage="Invalid e-mail address")]
[Required]
[StringLength(256, ErrorMessage = "The email address is too long")]
public string EmailAddress { get; set; }
and displayed on a registration form as:
<div>
*E-MAIL ADDRESS
@Html.EditorFor(m => regVM.EmailAddress)
@Html.ValidationMessageFor(m => regVM.EmailAddress)
</div>
This renders an HTML 5 "email" type input, which is what I want and my reason for including the DataTypeAttribute. However, if I enter the value "asdf" into the email input, it displays the validation error message: Please enter a valid email address.
Notice the attribute not only causes validation but also ignores the ErrorMessage I've provided (which, initially, I had not included under the belief this was not a validation attribute.) I am aware that you can use the EmailAddressAttribute to specify a custom validation message, but that's not my question.
I guess I'm looking to
- Add to the web knowledge base that although the attribute may not be intended for validation, it sure includes it (if that is, indeed, the case)
- Ask, "Is there is there a way to disable this validation?"
- Get thoughts on why, if it's going to validate, which makes sense since DataTypeAttribute inherits from ValidationAttribute, does it ignore the ErrorMessage parameter? This seems like a bug. It basically says, "I'm a validation attribute...but only a little bit".
Notes: I had the unobtrusive, client-side validation enabled during my tests.
Edit
So I decided to do another test. I removed jquery.validate.js and jquery.validate.unobtrusive.js from my page. Now I can submit an invalid e-mail address and ModelState.IsValid is true; it does not validate the e-mail address format. So maybe this is a bug with the client-side validation scripts.