samesite


【译】MVC3 20个秘方-(3)验证用户的输入

场景
你要确保你的表单捕获的数据包含你预期的数据,这些数据是基于你的数据库或模型设计。

解决方案

.NET 4.0 包含了一个新的数据注解命名空间,提供了一些有用的元数据属性类。这些类已经被应用到MVC3。

对于验证表单输入,下面的属性类可以用来提供各种各样
验证选项:RequiredAttribute,RegularExpressionAttribute,RangeAttribute和DataTypeAttribute。当需要自定义的验证的时候,MVC的3还支持改进ValidationAttribute类,允许开发人员定义的验证。

讨论

接下来的例子是要去扩展“code-first book“model,这个model是在前一“秘方”中创建的。

这个model将按照以下条件被更新:

1. 书名是必须的

2. ISBN是合法的

3. 书的摘要是必须的

4. 作者是必须的

5. 合法的价格(美元)

6. 合法的出版日期

以上6个验证中的5个可以由MVC 3 的内置方法完成。然而,第5个验证需要用一种不同的格式化-它需要一个自定义验证方法。

 public class Book
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
[IsbnValidation]
public string Isbn { get; set; }
[Required]
public string Summary { get; set; }
[Required]
public string Author { get; set; }
public string Thumbnail { get; set; }
[Range(1, 100)]
public double Price { get; set; }
[DataType(DataType.Date)]
[Required]
public DateTime Published { get; set; }
}
public class BookDBContext : DbContext
{
public DbSet<Book> Books { get; set; }
}

 

在上边的例子,[Required]数据注解被附加在每个字段上,表明这个字段是必须由用户提供。在ISBN number上 [IsbnValidation]特性也被添加了,这是通知MVC 3 IsbnValidation 必须调用IsValid操作,这个操作即将被创建.为了验证价格,[Range] 注解被应用。对于价格的验证,我们也可以用正则表达式特性 [RegularExpression] 来完成。

如下:

[RegularExpression (@"(\b[\d\.]*)")]

public double Price { get; set; }

最后,对于published date(出版日期)的验证,DataType特性告诉MVC这个字段的类型是一个日期类型。

 

一个合法ISBN的定义是:10-13个字符。为何合理的组织代码,自定义验证类将被放在一个单独的文件夹里。

右键点击项目:添加->新建文件夹。我们为这个文件夹命名为:Validations.在该文件夹点击右键。添加类:IsbnValidationAttribute.cs

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace MvcApplication.Validations
{
[AttributeUsage(AttributeTargets.Field |
AttributeTargets.Property, AllowMultiple = false,
Inherited = true)]
public class IsbnValidationAttribute :
System.ComponentModel.DataAnnotations.ValidationAttribute
{
/**
* This class is courtesy:
*
http://www.java2s.com/Open-Source/CSharp/
* Inversion-of-Control-Dependency-Injection/Spring.net/
* Spring/Validation/Validators/ISBNValidator.cs.htm
*
* This class is used for demonstration purposes
* of performing an ISBN validation. Should you
* wish to use this in your project, please
* consult the license agreement here:
*
http://www.apache.org/licenses/LICENSE-2.0
*
*/
private static readonly String SEP = "(?:\\-|\\s)";
private static readonly String GROUP = "(\\d{1,5})";
private static readonly String PUBLISHER = "(\\d{1,7})";
private static readonly String TITLE = "(\\d{1,6})";
static readonly String ISBN10_PATTERN =
"^(?:(\\d{9}[0-9X])|(?:" + GROUP + SEP + PUBLISHER +
SEP + TITLE + SEP + "([0-9X])))$";
static readonly String ISBN13_PATTERN =
"^(978|979)(?:(\\d{10})|(?:" + SEP + GROUP + SEP +
PUBLISHER + SEP + TITLE + SEP + "([0-9])))$";
public IsbnValidationAttribute() :
base("Invalid ISBN number")
{
}
public override bool IsValid(object value)
{
// Convert to string and fix up the ISBN
string isbn = value.ToString();
string code = (isbn == null)
? null :
isbn.Trim().Replace("-", "").Replace("", "");
// check the length
if ((code == null) || (code.Length < 10
|| code.Length > 13))
{
return false;
}
// validate/reformat using regular expression
Match match;
String pattern;
if (code.Length == 10)
{
pattern = ISBN10_PATTERN;
}
else
{
pattern = ISBN13_PATTERN;
}
match = Regex.Match(code, pattern);
return match.Success && match.Index == 0 &&
match.Length == code.Length;
}
}
}

创建完这个类记得在 book.cs添加命名空间引用:using MvcApplication.Validations;

上边的例子包含了一个标准的ISBN验证。这个验证是来自CSharp Open Source example。如果ISBN符合2个正则表达式中的一个。验证函数将返回true。否则返回false。需要用户重新输入

如果你在你的浏览器里转到图书创建页面。当你点击提交按钮。验证就被会触发。

 

另请参阅

DataAnnotations Namespace

 

译者注

关于用户验证,可以有更多方法,支持客户端/服务器端验证。

由于是翻译,我会尽量和原著保持一致,关于其他的验证方法会在后续的其他系列写出。

posted @ 2011-11-23 11:15  技术弟弟  阅读(3608)  评论(6编辑  收藏  举报