【问题标题】:how to? with knockout js validations如何?带有淘汰赛 js 验证
【发布时间】:2013-03-22 08:22:04
【问题描述】:

我已经开始使用带有http://ericmbarnard.github.com/Knockout-Validation/ 验证引擎的淘汰赛js 验证,但我不清楚如何执行以下操作:

1) 假设我想根据条件设置所需的特定字段。我怎么做? 例如
this.Username = ko.observable().extend({ required: true }); // 仅当 this.UserType = 2 等时才使 required = true...

2) 我在正在验证的字段旁边触发了验证消息。我只想在该字段旁边显示一个“*”,并在页面底部的验证摘要字段中显示错误消息。所有验证错误都应显示在那里。该怎么做?

3) 在表单验证通过之前,要避免表单提交。现在,我收到验证错误消息,但表单仍然被提交。所以我想我做错了什么。以下是我的代码:

 $(document).ready(function () {
var model;

// enable validation
ko.validation.init();


$.ajax({
    type: "POST",
    url: SERVER_PATH + '/jqueryservice/DataAccessService.asmx/GetData',
    async: false,
    data: "{ }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result, status) {
        model = new ViewModel(result);
        ko.applyBindings(model);
    },
    error: GetDataError

});

$('#submit').click(function () {
    var data = ko.toJS(model);
    delete data.Vehicles;
    delete data.CopyWeeks;
    delete data.SetupTotal;
    delete data.CloseTotal;

    var mappedItems = ko.utils.arrayMap(data.DailyItemList, function (item) {
        delete item.Add;
        delete item.Delete;
        return item;
    });
    data.DailyItemList = mappedItems;

    $.ajax({
        type: "POST",
        url: SERVER_PATH + '/jqueryservice/DataAccessService.asmx/ProcessData',
        async: false,
        data: ko.toJSON(data),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result, stat) {
            alert(success);
            return false;
        },
        error: function (e) {
            alert(e);
        }
    });
});

});

提前感谢您的帮助。

编辑: 我已经看到我可以按如下方式设置验证配置: ko.validation.configure({ 装饰元素:假, errorMessageClass: 'errorMsg', 插入消息:假, 解析输入属性:真, 消息模板:'sErrorMsg' }); ko.validation.init();

但我不确定如何定义我的错误消息模板“sErrorMsg”

【问题讨论】:

    标签: validation knockout.js required


    【解决方案1】:

    1)。假设我想根据条件设置所需的特定字段....

    对于这个 ko 验证包含一个native rule。您可以执行以下操作:

    var myObj = ko.observable().extend({ required: { 
                onlyIf: function() { 
                          //here you can place your codition and can return.. 
                          //true or false accordingly
                        } 
                }});
    

    2)。我在正在验证的字段旁边触发了验证消息..

    为此,您应该检查Validation Binding。在这个validationOptions 中可以为你完成这项工作。

    更新:这是一个小提琴,演示了根据您的要求使用messageTemplate 绑定。

    http://jsbin.com/ocizes/3/edit

    3)。在表单验证通过之前要避免表单提交......

    为此,您可以使用 group ,例如:

       yourViewModel.Errors = ko.validation.group(yourViewModel);
    

    现在,Errors 属性包含了你的 observables 的错误信息,如果有的话。因此,在提交表单之前,您可以检查以下内容:

          if(yourViewModel.Errors().length == 0) {
             //submit the form 
          }
          else {
             yourViewModel.Errors.showAllMessages();
    
             //this line shows all the errors if validation fails, 
             //but you can omit this.
          }
    

    【讨论】:

    • 好的,我得到了 1) 和 3) 的工作。非常感谢。但是2对我来说仍然是个谜。我浏览了您链接的 Github 文档,但不知道如何使用它。你能举个例子吗?另外, yourViewModel.Errors.showAllMessages() 有什么作用?我无法让它工作。
    • 你能帮我解决这个问题吗?
    • 是的,请检查更新的答案和小提琴,如果有任何进一步的疑问,请告诉我。
    • 嘿,非常感谢您为设置小提琴并解释它所带来的所有麻烦,谢谢大家。
    猜你喜欢
    • 2015-02-28
    • 2012-02-18
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2014-10-13
    • 2011-08-09
    • 2015-02-01
    • 2012-11-04
    相关资源
    最近更新 更多