Fred
Fred

Reputation: 12826

EmailAddress or DataType.Email attribute

What is the difference between the [EmailAddress] and the [DataType(DataType.Email)] attribute?

What is the difference between the [Phone] and [DataType(DataType.PhoneNumber)] attribute?

[EmailAddress]
public string Email { get; set; }

[Phone]
public string Phone { get; set; }

and

[DataType(DataType.Email)]
public string Email { get; set; }

[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }

Are these the same or is there any difference? What is the difference? Which is the preferred way? When should which be used?

Upvotes: 11

Views: 3906

Answers (3)

Jakub Januszkiewicz
Jakub Januszkiewicz

Reputation: 4418

DataTypeAttribute changes the type attribute of the <input> elements rendered by MVC.

@David is right that EmailAddressAttribute derives from DataTypeAttribute, so all functionality you get with [DataType(DataType.Email)] is also present when you use [EmailAddress]. Both attributes cause MVC to render HTML <input type="email"> elements.

However, EmailAddressAttribute adds server-side validation on top of that. I.e. there is no server-side validation if you only use DataTypeAttribute! You can easily test your model with each of those attributes. For each of them, you should get client-side validation and it shouldn't be possible to submit the form with invalid email address. However, if you change the <input> element type to text (via Firebug or whatnot), you will remove that validation and will be able to submit the form with whatever text you like. Then, put a breakpoint in the action invoked by submitting the form and examine the value of ModelState.IsValid - when you use DataTypeAttribute, it is true. When you use EmailAddressAttribute, it is false. This is because the latter adds some regex-based server-side validation.

Conclusion: you should use EmailAddressAttribute et al., otherwise you're not really doing the validation on your end and rely on the client to do it (a Bad Thing™).

Of course you can also use DataTypeAttribute and implement your own server-side validation (e.g. because the one in EmailAddressAttribute doesn't work for you for whatever reason).

Upvotes: 17

Miniver Cheevy
Miniver Cheevy

Reputation: 1677

As far as their implementation in MVC, I believe that there was a time where EmailAddressAttribute and PhoneAttribute were validation related and would render input type="text" and that the the DataType attributes were rendering related. However after some quick testing that is no longer the case.

They do in fact seem to be different implementations of the same functionality. They both render as type="email" and both validate email addresses. The DataType.Email validation is slightly different from the EmailAddressValidation, it does not go through jquery.validate.unobtrusive.js to wire up validation and for that reason I would lean towards the EmailAddressAttribute, but likely there will be no functional difference.

Upvotes: -1

David
David

Reputation: 218960

There's probably no difference, other than the static type of the attribute. Looking at the documentation, classes like EmailAddressAttribute and PhoneAttribute inherit from DataTypeAttribute, so they're likely just convenient delegates to the more generic parent with its properties already set.

Note also that this doesn't really have anything to do with MVC, this is the System.ComponentModel functionality. MVC uses it, but doesn't own it.

Upvotes: -1

Related Questions