使用xVal验证标题和用户名的唯一组合

时间:2009-12-10 00:06:06

标签: xval

我有一个用于创建/编辑文章的表单。每篇文章都与用户相关联。

发布文章后,每篇文章的链接由用户名和文章标题组成({userName} / {articleTitle}应该是唯一的组合):

/articles/{userName}/{articleTitle}

文章类:

public class Article
{
    public int ArticleId { get; set; }

    [Required(ErrorMessage = "Please enter title")]
    public string Title { get; set; }         

    [Required(ErrorMessage = "Please select a user")]
    public int UserId { get; set; }        
}

查看型号:

public class ArticleFormViewModel
{
    public Article Article { get; set; }
    public SelectList Users { get; set; }

    public ArticleFormViewModel(Article article, Dictionary<int, string> allUsers)
    {
        Article = article;

        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem() { Value = "", Text = "Please select a user" });

        foreach (var user in allUsers)
        {
            list.Add(new SelectListItem() { Value = user.Key.ToString(), Text = user.Value });
        }

        Users = new SelectList(list, "Value", "Text", Article.UserId);
    }
}

查看:

<div id="validationSummary">
    <%= Html.ValidationSummary("Please correct the errors and try again.") %>
</div>

<% using (Html.BeginForm()) {%>               
    <%= Html.Hidden("ArticleId", Model.Article.ArticleId) %>

    <fieldset>
    <legend>Article</legend>            
    <ul>
        <li>
            <label for="UserId">User: <%= Html.ValidationMessage("UserId", "*")%></label>
            <%= Html.DropDownList("UserId", Model.Users) %>
        </li>
        <li>
            <label for="Title">Title: <%= Html.ValidationMessage("Title", "*") %></label>
            <%= Html.TextBox("Title", Model.Article.Title) %>
        </li>            
    </ul>
    <input type="submit" value="Save" />            
    </fieldset>
<% } %>

<%= Html.ClientSideValidation(typeof(Article))
                .AddRule("Title", new RemoteRule(Url.Action("ValidateTitle")))
                .UseValidationSummary("validationSummary", "Please correct the errors and try again.")%> 

我正在使用xVal进行验证。

ValidateTitle - 是一个控制器操作,用于验证{userName} / {articleTitle}是唯一的。它使用Ajax工作。

当我编辑标题时,一切正常,但是当我在选择列表中更改用户时,我遇到了问题。如果title1对user1无效,并且我选择user2,则之前的错误消息仍然存在,我无法检查user2的标题是否有效。

我可以像使用标题一样验证用户名,但是会出现2个错误,表示用户名和标题组合无效的情况。

标题错误和用户错误应该同步,但如何?

或许我可以采用另一种方式处理标题和用户列表?

1 个答案:

答案 0 :(得分:0)

xVal生成验证规则(用于jQuery验证插件):

<script type="text/javascript">xVal.AttachValidator(null, 
{"Fields":[{"FieldName":"ArticleId","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},
{"FieldName":"Title","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Please enter title"}, 
{"RuleName":"Remote","RuleParameters":{"url":"/articles/ValidateTitle"}}]},
{"FieldName":"UserId","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Please select a user"}]}]}, 
{"ValidationSummary":{"ElementID":"validationSummary","HeaderMessage":"Please correct the errors and try again."}})</script>  

现在只有一个条件:应该从选择列表中选择用户。

当title + user1无效且我从列表中选择user2时,我不知道如何隐藏(重置)标题错误消息。

相反:在我选择用户并编辑标题后隐藏用户错误消息。