如何仅在表单字段更新时触发Knockout验证

时间:2017-02-09 22:27:07

标签: knockout.js knockout-validation

我目前正在处理一个表单,其中有三个字段供用户输入:报告,保留和拒绝。业务规则是,它们中的任何一个都可以为null,但如果放入任何值,那么它必须是Report<保持<下降。使用knockout.validation是相当不错的,除非我将值放在一个输入框中,所有验证都将被验证并显示在页面上,这令人困惑。我想要做的是,如果用户输入一个报告值,然后输入一个拒绝值,然后在UI上,错误消息应显示在拒绝输入下,如果是这样的话,“拒绝值必须大于报告值”,但我不希望在报告输入下看到另一条错误消息,反之亦然。

enter image description here

这是我的代码:

UI:

<div class="form-group" data-bind="validationElement: Report">
    <label class="control-label" for="inputReport">Report</label>
    <div class="controls">
        <input id="inputReport" class="form-control" type="text" data-bind="value: Report"  />
    </div>
    <div class="help-block with-errors error">
        <span data-bind="validationMessage: Report"></span>
    </div>
</div>

<div class="form-group" data-bind="validationElement: Hold">
    <label class="control-label" for="inputHold">Hold</label>
    <div class="controls">
        <input id="inputHold" class="form-control" type="text" data-bind="value: Hold"/>
    </div>
    <div class="help-block with-errors error">
        <span data-bind="validationMessage: Hold"></span>
    </div>
</div>


<div class="form-group" data-bind="validationElement: Decline">
    <label class="control-label" for="inputDecline">Decline</label>
    <div class="controls">
        <input id="inputDecline" class="form-control" type="text" data-bind="value: Decline"/>
    </div>
    <div class="help-block with-errors error">
        <span data-bind="validationMessage: Decline"></span>
    </div>
</div>

JS:

function ViewModel() {
var self = this;

self.Report = ko.observable(null)
.extend({
    max: 100,
    min: 0,
    required: false
});

self.Hold = ko.observable(null)
    .extend({
        max: 100,
        min: 0,
        required: false
    });
self.Decline = ko.observable(null)
    .extend({
        max: 100,
        min: 0,
        required: false
    });

self.Report.extend({
        validation: [
            {
                validator: function(val) {
                    if (self.Hold() && val > self.Hold()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be less than Hold value'
            }, {
                validator: function(val) {
                    if (self.Decline() && val > self.Decline()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be less than Decline value'
            }
        ]
    });


self.Hold.extend({
        validation: [
            {
                validator: function (val) {
                    if (self.Report() && val < self.Report()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be more than Report value'
            }, {
                validator: function (val) {
                    if (self.Decline() && val > self.Decline()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be less than Decline value'
            }
        ]
    });


self.Decline.extend({
        validation: [
            {
                validator: function(val) {
                    if (self.Hold() && val < self.Hold()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be more than Hold value'
            }, {
                validator: function(val) {
                    if (self.Report() && val < self.Report()) {
                        return false;
                    }
                    return true;
                },
                message: 'Must be less than Report value'
            }
        ]
    });
 }

ko.validation.init({
   insertMessages: false,
   decorateElement: true,
   errorElementClass: 'has-error',
   errorMessageClass: 'help-inline'
 },true);


$(document).ready(function () {
   ko.applyBindings(new LimitManagerAdminViewModel());
 });

在Anonymous验证器中,我尝试在Report,Hold和Decline上检查isModified,无论它们始终如何。如果有人可以告诉我如何只显示输入用户的消息(当标签出来时),那就太棒了。

感谢您的帮助!

0 个答案:

没有答案