我可以将客户端验证限制在特定字段吗?

时间:2012-11-09 23:15:36

标签: asp.net-mvc-3 unobtrusive-validation client-side-validation

我有一个表格,我成功地使用[remote]注释进行不显眼的验证。 我在模板中有其他字段,模型中有[required]注释,但我不希望这些字段的客户端验证。

我只想要[必填]字段的服务器端验证

我还没有找到解决方案,我想知道它是否容易实现?

编辑:

我的代码的一部分

我的部分模型:

我想要第一个字段:“Email”使用Unobtrusive-validation,第二个字段:“PasswordHash”只使用服务器端验证,即使它有[required]注释。

最后,我不希望所有表单字段都出现AJAX错误消息。

[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "EMAIL_REQUIRED")]
[Display(Name = "EMAIL_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
[Remote("EmailExists", "User", "An account with this email address already exists.")]
public string Email { get; set; }

[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "PASSWORDHASH_REQUIRED")]
[DataType(DataType.Password)]
[Display(Name = "PASSWORDHASH_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
public string PasswordHash { get; set; }

Controller中的部分操作 服务器端验证:

    [HttpPost]
    public ActionResult Register(User user)
    {

        if (ModelState.IsValid )
        {   

            DataContext.User.Add(user);
            DataContext.SaveChanges();

            return RedirectToAction("Index");
        }

        return View(user);
    }

Ajax验证:

public JsonResult EmailExists(string email)
{
    User user = DataContext.User.SingleOrDefault(x => x.Email == email);

    return user == null ?
        Json(true, JsonRequestBehavior.AllowGet) :
        Json(
            string.Format("an account for address {0} already exists.",
            email), JsonRequestBehavior.AllowGet);
}

部分观点:

 @using (Html.BeginForm())
 {
    @Html.ValidationSummary(true)


    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PasswordHash)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PasswordHash)
        @Html.ValidationMessageFor(model => model.PasswordHash)
    </div>

    <p>
        <input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
    </p>
 }

当我点击sumbit按钮时,我希望进行服务器端验证。

对于像Email这样的特定领域,我想要一个Ajax验证。

1 个答案:

答案 0 :(得分:0)

可能有更好的方法,但实现此目的的一种方法是执行以下操作

@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { id = "registerForm" }))
 {
    @Html.ValidationSummary(true)


    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PasswordHash)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PasswordHash)
        @Html.ValidationMessageFor(model => model.PasswordHash)
    </div>

    <p>
        <input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
    </p>
 }

<script type="text/javascript">

    // add the rules for the fields that you want client-side validation on
    // leave out the fields that you dont want client-side validation. they will be validated 
    // on server side.
    $("#registerForm").validate({
        rules: {
            Email: { required: true }
        },
        messages: {
            Email: { required : "Email is required" }
        }
    });
</script>