knockout.js KnockoutJS Required(ifonly)在可观察到的变化时不被接受

fhity93d  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(115)

我有两个字段需要根据模型中的另一个字段设置为必填字段。第一个字段的功能正常,但是第二个字段(类似的逻辑)不荣誉必填属性。我已经验证了当可观察对象发生变化时,onlyif代码会触发。但是,如果必填字段没有填写,表单将允许提交。
JS代码

//Works As Expected
           self.model.RecentLocations.LastDayOnSite.extend({
                required: {
                    onlyIf: function () {
                        return ((!self.model.RecentLocations.IsLastDayOnSiteNA()) && (self.model.CaseType() != 'Quarantine'));
                    }
                }
            });

//Not Requiring Field as expected.
            self.model.ContactTracingStartDate.extend = ko.observable().extend({
                required: {
                    onlyIf: function () {
                        return (self.model.IsContactTracingRequired() == "Y");
                    }
                }
            });

HTML程式码

//Works As Expected
                    <div class="col-md-2 form-group">
                        <i id="lastDayOnSite-asterisk" class="fas fa-asterisk fa-fw" style="font-size: 7px; color:red; vertical-align:super" data-bind="hidden: (model.RecentLocations.IsLastDayOnSiteNA() || model.CaseType() === 'Quarantine')"></i>
                        <label for="lastDayOnSite-datepicker_nfd">Last Day on Site</label>
                        <input type="text" class="form-control datepicker_nfd" id="lastDayOnSite-datepicker_nfd" data-bind="value: model.RecentLocations.LastDayOnSite, preventFutureDate: model.RecentLocations.LastDayOnSite, disable: model.RecentLocations.IsLastDayOnSiteNA()" data-emessage="Last Day on Site" placeholder="Date">
                    </div>

//Not Requiring Field as expected.
                    <div class="col-md-2 form-group">
                        <i id="contactTracingStartDate-asterisk" class="fas fa-asterisk fa-fw" style="font-size: 7px; color:red; vertical-align:super" data-bind="visible: (model.IsContactTracingRequired() === 'Y')"></i>
                        <label for="contactTracingStartDate-datepicker_nfd">Contact Tracing Start Date</label>
                        <input type="text" class="form-control datepicker_nfd" id="contactTracingStartDate-datepicker_nfd" 
                               data-bind="value: model.ContactTracingStartDate, 
                              preventFutureDate: model.ContactTracingStartDate, enable: (model.IsContactTracingRequired() === 'Y')" data-emessage="Contract Tracing Start Date" placeholder="Date">
                    </div>

不知道我错过了什么,但我是相当新的KnockoutJS,但我不能看到哪里断开。任何帮助或建议将不胜感激。

a2mppw5e

a2mppw5e1#

答案是更改代码行

self.model.ContactTracingStartDate.extend = ko.observable().extend({

收件人:

self.model.ContactTracingStartDate.extend({

问题是observable被ko.observable().extend重置了,而不是仅仅扩展了现有的observable。

相关问题