I am trying MVC's datatype attributes, and just created a simple scenario like the following:
The View:
@model MVC4.Models.Model
@{
ViewBag.Title = "DataTypeAttribute";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>DataTypeAttribute</h2>
@using (Html.BeginForm("SubmitData", "Home"))
{
<div>
@Html.ValidationSummary()
</div>
@Html.EditorFor(m => m.Email)
<br />
<br />
@Html.EditorFor(m => m.PostalCode)
<br />
<br />
@Html.EditorFor(m => m.TextOnly)
<br />
<br />
<button type="submit">Submit</button>
}
The Model:
public class Model
{
[DataType(DataType.EmailAddress)]
[Required]
//[EmailAddress]
public string Email { get; set; }
[DataType(DataType.PostalCode)]
public string PostalCode { get; set; }
public string TextOnly { get; set; }
}
"SubmitData" is just a controller that, returns View(..., model) if ModelState.IsValid is false.
Although posts like this do a good job in tackling the differences between Html.TextBoxFor and Html.EditorFor, I could not find an answer as to why validation for the datatype EmailAddress will not work when using TextBoxFor. I did find people mentioning TextBoxFor does not take metadata into account, while EditorFor does.
But does this make sense ? So TextBoxFor does not offer support for client validations ?!
I wonder what is the reason for the difference between the two ?
TextBoxFor()
does work with validations.[DataType(DataType.EmailAddress)]
is not a validation attribute. Its an attribute that determines the type of input to display by setting thetype
attribute in the rendered html. For example<input type="text" ..>
,<input type="date" ..>
,<input type="email" ..>
in order to render the browsers implementation of a HTML4 datepicker, email input etc.. It works only forEditorFor()
becauseTextBoxFor()
as its name suggest generates and input withtype="text"
If you want validation for an email address, then you use the
[EmailAddress]
attribute on your property.Edit (further to the comments)
One of the features of HTML5 is the ability to validate user data without relying on scripts. One such form of browser validation is using the
type
attribute. The use of[DataType(DataType.EmailAddress)]
on a property that is rendered with@Html.EditorFor()
addstype="email"
to theinput
element. From the MDN documentationIf your are currently seeing a validation error message associated with the property, and you have not added the
[EmailAddress]
attribute, then it means thatjquery.validate.js
is not loaded and you are seeing the browsers error message associated withtype="email"
.When
jquery.validate.js
is loaded (correctly), thenovalidate="novalidate"
attribute is added to theform
element, which specifies that form is not to be validated (using the HTML5 validation) when submitted. The relevant code fromjquery.validate.js
is (approx line 35)This is added to prevent possible confusion between error messages displayed by browser validation and jquery unobtrusive validation.
As to why
DataTypeAttribute
attribute inherits ValidationAttribute when it does not actually do validation, from Brad Wilson himself in this answer