KnockoutJS Required(ifonly)在可观察到的变化时不被尊重

KnockoutJS Required (ifonly) not being honored when observable changes

根据我模型中的另一个字段,我需要 2 个字段。第一个字段按预期运行,但是第二个字段(类似的逻辑)不支持必需的属性。我已经验证了 onlyif 代码在 observable 更改时触发。但是,如果未填写必填字段,则表单允许提交。

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 还很陌生,但我看不到断开连接的位置。任何帮助或建议将不胜感激。

答案是更改代码行

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

收件人:

self.model.ContactTracingStartDate.extend({

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