使用jQuery UI Dialog的验证插件仅触发验证一次

时间:2013-05-14 04:23:52

标签: jquery asp.net jquery-ui validation knockout.js

我有一个ASP.NET Web窗体,其字段绑定到使用KnockoutJS的数据模型。我正在使用Knockout验证插件进行验证。

用户可以在他们的应用程序中添加多个电话号码,我正在使用UI对话框来提示他们。我的问题是,当验证被触发时,它让用户知道:“必须输入超过2位数”,我在我的模型上应用了最大属性的电话号码。验证仅触发一次,用户只需按两次接受即可绕过。

我附上了一个jsfiddle来显示问题。

<h3>Phone Number</h3>

<input type="button" value="Add New Phone" data-bind="click: AddPhone" />
<table>
<thead>
    <tr>
        <th>Type</th>
        <th>Number</th>
        <th></th>
        <th></th>
    </tr>
</thead>
<tbody data-bind="foreach: PhoneNumbers">
    <tr>
        <td data-bind="text: PhoneNumber_PhoneTypeName"></td>
        <td data-bind="text: PhoneNumber_Number"></td>
        <td data-bind="click: $root.EditPhone">Edit</td>
        <td data-bind="click: $root.DeletePhone">Delete</td>
    </tr>
</tbody>
</table>
<div data-bind="jqDialog: { autoOpen: false, resizable: false, modal: true, title:      'Phone Number' },
                template: { name: 'addPhoneNumberDialog', data: EdittingPhone, 'if':    EdittingPhone },
                openDialog: EdittingPhone"></div>


<script id="addPhoneNumberDialog" type="text/html">
    <div class="addPhoneNumberDialog">                    
    <p><span>Type:</span> <asp:DropDownList runat="server" ID="ddlApplicantPhoneType"    data-bind="value: PhoneNumber_PhoneTypeID, selectedText: PhoneNumber_PhoneTypeName">   </asp:DropDownList></p> 
    <p><span>Number:</span> <input id="txtApplicantPhone" data-bind="value: PhoneNumber_Number" class="required phoneUS" /></p>                    
    <input type="button" class="acceptButton" value="Accept" data-bind="jqButton: {}, click: $root.OnAcceptPhoneEdit" />
    <input type="button" value="Cancel" data-bind="jqButton: {}, click: $root.OnCancelPhoneEdit" />
    </div>
</script>

//custom binding to initialize a jQuery UI dialog
ko.bindingHandlers.jqDialog = {
init: function (element, valueAccessor) {
    var options = ko.utils.unwrapObservable(valueAccessor()) || {};

    //handle disposal
    ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
        $(element).dialog("destroy");
    });

    $(element).dialog(options);
}
};

//custom binding handler that opens/closes the dialog
ko.bindingHandlers.openDialog = {
update: function (element, valueAccessor) {
    var value = ko.utils.unwrapObservable(valueAccessor());
    if (value) {
        $(element).dialog({
            appendTo: $('form:first'),
            open: function () {
                $('.datepicker').datepicker();
            }
        });
        $(element).dialog('open');
    } else {
        $(element).dialog("close");
    }
}
};

var Application = function () {
var self = this;
this.Application_USCitizen = ko.observable(false);
this.Application_FirstName = ko.observable('').extend({
    required: true
});
this.Application_MiddleName = ko.observable('');
this.Application_LastName = ko.observable('');

this.PhoneNumbers = ko.observableArray([]);

this.SelectedPhone = undefined;
this.EdittingPhone = ko.observable();
this.AddPhone = function () {
    self.EdittingPhone(new PhoneNumber());
}
this.EditPhone = function (phone) {
    self.SelectedPhone = phone;
    self.EdittingPhone(new PhoneNumber().Copy(phone));
}
this.DeletePhone = function (phone) {
    self.PhoneNumbers.remove(phone);
}
this.OnAcceptPhoneEdit = function () {
    var editted = self.EdittingPhone();
    if (self.SelectedPhone != undefined) {
        self.SelectedPhone.Copy(editted);
    } else {
        self.PhoneNumbers.push(editted);
    }

    self.SelectedPhone = undefined;
    self.EdittingPhone(undefined);
}
this.OnCancelPhoneEdit = function () {
    self.SelectedPhone = undefined;
    self.EdittingPhone(undefined);
}
};

var PhoneNumber = function () {
var self = this;
this.PhoneNumber_PhoneTypeID = ko.observable(0);
this.PhoneNumber_PhoneTypeName = ko.observable('');
this.PhoneNumber_Number = ko.observable('').extend({
    max: 2
});

this.Copy = function (phone) {
    self.PhoneNumber_PhoneTypeID(phone.PhoneNumber_PhoneTypeID())
    self.PhoneNumber_PhoneTypeName(phone.PhoneNumber_PhoneTypeName())
    self.PhoneNumber_Number(phone.PhoneNumber_Number());

    return self;
}
};

var vm = new Application();
ko.applyBindings(vm);

http://jsfiddle.net/bjk964/wc7Vf/

1 个答案:

答案 0 :(得分:1)

验证库不会自动停止触发按钮,您需要使用ko.validation.group并检查点击处理程序是否存在任何验证错误

http://jsfiddle.net/wc7Vf/1/

var PhoneNumber = function () {
    var self = this;
    this.PhoneNumber_PhoneTypeID = ko.observable(0);
    this.PhoneNumber_PhoneTypeName = ko.observable('');
    this.PhoneNumber_Number = ko.observable('').extend({
        max: 2
    });

    this.Copy = function (phone) {
        self.PhoneNumber_PhoneTypeID(phone.PhoneNumber_PhoneTypeID())
        self.PhoneNumber_PhoneTypeName(phone.PhoneNumber_PhoneTypeName())
        self.PhoneNumber_Number(phone.PhoneNumber_Number());

        return self;
    }

    this.errors = ko.validation.group(this);
};