MVC Validation

Server-side validation

Server-side validation should be done whether we validate on the client or not. Users could disable JavaScript or do something unexpected to bypass client-side validation, and the server is the last line of defense protecting our data from dirty input. Some
validation rules require server-side processing—network topology might require that only the server has access to some external resource required to validate input.

  • Validation with Data Annotations

Data Annotations, introduced with the .NET 3.5 SP1 release, are a set of attributes and classes defined in the System.ComponentModel.DataAnnotations assembly that allow you to decorate your classes with metadata. This metadata describes a set of rules that can be used to determine how a particular object should be validated.

The Data Annotations attributes used for validation

Attribute Description
CompareAttribute

Compares the value of two model properties—if they are equal,
validation succeeds

RemoteAttribute

Instructs jQuery Validate, the default client validation library, to
call an action on the server to perform server-side validation
with a client-side experience

RequiredAttribute Specifies that a data field value is required
RangeAttribute Specifies the numeric range constraints for the value of a data field
RegularExpressionAttribute

Specifies that a data field value must match the specified
regular expression

StringLengthAttribute

Specifies the maximum number of characters that are allowed
in a data field

 

  • Display potential validation error messages
    • If we’re using the model templates, validation messages are already included in the template.The default editor model templates generate a user interface that includes side by side input elements and validation messages.
      <h2>Edit</h2>
      @using (Html.BeginForm("Edit", "Home"))
      {
           @Html.EditorForModel()
           <button type="submit">Submit</button>
      }
    • ValidationSummary & ValidationMessage - The ValidationSummary extension provides a summary list of validation errors, usually displayed at the top of the form. For validation errors for specific model properties, we can use the ValidationMessage and expression-based ValidationMessageFor methods.

 

Client-side validation

  • Import URLs of scripts - Because each JavaScript library builds on others, it’s important that the files be included in the correct order.
<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

 

  • Enable Client-side validation in Web.config
 <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>
  • RemoteAttribute

A new validation attribute in ASP.NET MVC 3 is RemoteAttribute. Decorating a model property with this attribute will instruct jQuery Validate to make an HTTP request to a given action method for server-side checking. The result is transmitted back to the client, and an error message will be displayed before the form is submitted.

posted @ 2012-08-20 17:22  HelloWorld.Michael  阅读(481)  评论(0编辑  收藏  举报