Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the DataTypeAttribute on a model do validation in MVC 3?

The default ASP.net MVC 3 Internet Application template includes the following model:

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

In the Account/Register action it is asking for an email address, but it seems you can type anything in this field and it will accept it.

Does the DataType(DataType.EmailAddress) actually trigger validation? It seems like it does not. If it doesn't validate the type, then what is its purpose?

like image 377
Travis Avatar asked Jul 01 '11 16:07

Travis


People also ask

How does ValidationMessageFor work in MVC?

ASP.NET MVC: ValidationMessageFor ValidationMessageFor() is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object. Visit MSDN to know all the overloads of ValidationMessageFor() method.

Is data annotation client side validation?

Note: By default, the validation done using Data Annotation attributes is Server Side. And hence to make it work Client Side, the Client Side validation must be enabled.

How do I know if a model is valid or not?

The state of the submitted Model is checked using ModelState. IsValid property and if the Model is valid then the value of the Name property is set in a ViewBag object.


2 Answers

So far as I'm aware, the DataType attribute is used for formatting but only when you use @Html.EditorFor(model => model.Field).

Examples from model fields, cshtml and the resulting HTML:

Model fields:

[Required]
[DataType(DataType.Text)]
[Display(Name = "Name")]
public string Name { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Description")]
public string Desc { get; set; }

Cshtml:

<div class="form-section">
    @Html.LabelFor(x => x.Name)
    @Html.EditorFor(x => x.Name)
</div>

<div class="form-section">
    @Html.LabelFor(x => x.Password)
    @Html.EditorFor(x => x.Password)
</div>

<div class="form-section">
    @Html.LabelFor(x => x.Desc)
    @Html.EditorFor(x => x.Desc)
</div>

Resulting HTML:

<div class="form-section">
    <label for="Name">Name</label>
    <input class="text-box single-line" id="Name" name="Name" type="text" value="">
</div>
<div class="form-section">
    <label for="Password">Password</label>
    <input class="text-box single-line password" id="Password" name="Password" type="password" value="">
</div>
<div class="form-section">
    <label for="Desc">Description</label>
    <textarea class="text-box multi-line" id="Desc" name="Desc"></textarea>
</div>

I know it's an old post, but maybe I can shine a light on the situation for others that come this way

EDIT:

There may be some validation attached to these but only client side. Do not rely on these attributes for actual validation, this should always be done server side (client side is only for user experience)

like image 90
JakeJ Avatar answered Sep 22 '22 20:09

JakeJ


There is NO validation associated with the DataType attributes in the current release of MVC3.

New attributes deriving from this attribute are shown in MVC Futures and add this validation process. For example, EmailAddressAttribute.

like image 37
Christophe Argento Avatar answered Sep 26 '22 20:09

Christophe Argento