我正在使用 FluentValidation 和 ASP.NET MVC 3。
我只有几个关于验证的问题。
我对我的视图模型设置进行了验证,如下所示:
public NewsViewModelValidator()
{
// Title is required
RuleFor(x => x.Title)
.NotEmpty()
.WithMessage("Title is required");
// Title must be less than or equal to 100 characters
RuleFor(x => x.Title)
.Length(1, 100)
.WithMessage("Title must be less than or equal to 100 characters");
}
这是必须这样做的方式,还是我需要将其包含在 1 条语句中?
我需要对 Title 属性进行 2 次验证,命名它必须是必填字段并且不能超过 100 个字符。下面是触发验证后的源码:
<td valign="top"><b>Title: *</b></td>
<td>
<input class="input-validation-error" data-val="true" data-val-length="Title must be less than or equal to 100 characters" data-val-length-max="100" data-val-length-min="1" data-val-required="Title is required" id="Title" max="100" name="Title" size="100" type="text" value="" /><br>
<span class="field-validation-error" data-valmsg-for="Title" data-valmsg-replace="true">Title is required</span>
</td>
它使用 2 个不同的类,每个验证类型一个。为什么是这样?现在我的表格不一致。我有另一个必需的输入字段,用于此的类是 input-validation-error,上面是 field-validation-error。
如何启用客户端验证?有人告诉我,如果我在 web.config 中设置了以下代码,那么客户端会自动打开:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
不会触发客户端验证。我是否需要包含 jQuery 库或者这是为我完成的?