Knockout ValidationMessage 绑定不显示消息
Knockout ValidationMessage binding not displaying message
我正在尝试对表单的多个部分使用 knockout.validation
。其中大部分工作正常,可以使用内置的消息附加程序,但我有 1 个特定字段,我需要自己手动将消息添加到 DOM.
我在让它工作时遇到了一个真正的问题,因为如果字段没有发生变化,我根本无法显示文本。我模拟了一个非常小的例子来尽可能接近更大的形式。
我看到的行为:
- 单击
submit
可正确运行验证(在输入周围添加红色边框)但它不会像应有的那样显示错误消息。
- 在密码框中输入内容,然后将其清空,然后点击提交即可正常显示错误消息和红色边框。
由于我正在尝试引入一个必填字段 - 我无法保证该字段曾经被修改过,所以我需要确保第一个案例有效。谁能看出我做错了什么?
ko.validation.init({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
decorateInputElement: true,
decorateElementOnModified: false,
errorElementClass: "is-invalid",
messageTemplate: "inline_error"
}, true);
const viewModel = {};
viewModel.submitted = ko.observable(false);
viewModel.clicked = () => { viewModel.submitted(true); };
const onlyIf = ko.computed(() => viewModel.submitted());
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf,
}
})
};
ko.applyBindings(viewModel);
.is-invalid {
border: thick solid red;
}
.text-danger {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>
<div id="domTest">
<button type="button" data-bind="click: clicked">Submit</button>
<input type="password" class="form-control" id="password" placeholder="Password" data-bind="validationOptions:{ insertMessages: false }, textInput: user.password">
<div class="text-danger error-message" data-bind="validationMessage: user.password" />
</div>
在这个 Github 问题中,validationMessage does not run on initial binding,stevegreatrex 说您需要将 messagesOnModified
更改为 false:
messagesOnModified: false
并且从这个问题的答案Knockout Validation onlyif object no function and pattern validation
(由 steve-greatrex 撰写)您需要更改 onlyIf
计算:
viewModel.onlyIf = ko.computed(() => viewModel.submitted());
然后将 viewModel.user
中的 属性 onlyIf
更改为指向计算的 viewModel.onlyIf
:
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf: viewModel.onlyIf(),
}
})
};
希望对您有所帮助。
ko.validation.init({
registerExtenders: true,
//messagesOnModified: true,
messagesOnModified: false,
insertMessages: true,
decorateInputElement: true,
decorateElementOnModified: false,
errorElementClass: "is-invalid",
messageTemplate: "inline_error"
}, true);
const viewModel = {};
viewModel.submitted = ko.observable(false);
viewModel.clicked = () => { viewModel.submitted(true); };
viewModel.onlyIf = ko.computed(() => viewModel.submitted());
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf: viewModel.onlyIf(),
}
})
};
ko.applyBindings(viewModel);
.is-invalid {
border: thick solid red;
}
.text-danger {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>
<div id="domTest">
<button type="button" data-bind="click: clicked">Submit</button>
<input type="password" class="form-control" id="password" placeholder="Password" data-bind="validationOptions:{ insertMessages: false }, textInput: user.password">
<div class="text-danger error-message" data-bind="validationMessage: user.password" />
</div>
我正在尝试对表单的多个部分使用 knockout.validation
。其中大部分工作正常,可以使用内置的消息附加程序,但我有 1 个特定字段,我需要自己手动将消息添加到 DOM.
我在让它工作时遇到了一个真正的问题,因为如果字段没有发生变化,我根本无法显示文本。我模拟了一个非常小的例子来尽可能接近更大的形式。
我看到的行为:
- 单击
submit
可正确运行验证(在输入周围添加红色边框)但它不会像应有的那样显示错误消息。 - 在密码框中输入内容,然后将其清空,然后点击提交即可正常显示错误消息和红色边框。
由于我正在尝试引入一个必填字段 - 我无法保证该字段曾经被修改过,所以我需要确保第一个案例有效。谁能看出我做错了什么?
ko.validation.init({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
decorateInputElement: true,
decorateElementOnModified: false,
errorElementClass: "is-invalid",
messageTemplate: "inline_error"
}, true);
const viewModel = {};
viewModel.submitted = ko.observable(false);
viewModel.clicked = () => { viewModel.submitted(true); };
const onlyIf = ko.computed(() => viewModel.submitted());
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf,
}
})
};
ko.applyBindings(viewModel);
.is-invalid {
border: thick solid red;
}
.text-danger {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>
<div id="domTest">
<button type="button" data-bind="click: clicked">Submit</button>
<input type="password" class="form-control" id="password" placeholder="Password" data-bind="validationOptions:{ insertMessages: false }, textInput: user.password">
<div class="text-danger error-message" data-bind="validationMessage: user.password" />
</div>
在这个 Github 问题中,validationMessage does not run on initial binding,stevegreatrex 说您需要将 messagesOnModified
更改为 false:
messagesOnModified: false
并且从这个问题的答案Knockout Validation onlyif object no function and pattern validation
(由 steve-greatrex 撰写)您需要更改 onlyIf
计算:
viewModel.onlyIf = ko.computed(() => viewModel.submitted());
然后将 viewModel.user
中的 属性 onlyIf
更改为指向计算的 viewModel.onlyIf
:
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf: viewModel.onlyIf(),
}
})
};
希望对您有所帮助。
ko.validation.init({
registerExtenders: true,
//messagesOnModified: true,
messagesOnModified: false,
insertMessages: true,
decorateInputElement: true,
decorateElementOnModified: false,
errorElementClass: "is-invalid",
messageTemplate: "inline_error"
}, true);
const viewModel = {};
viewModel.submitted = ko.observable(false);
viewModel.clicked = () => { viewModel.submitted(true); };
viewModel.onlyIf = ko.computed(() => viewModel.submitted());
viewModel.user = {
password: ko.observable().extend({
required: {
message: `password is required.`,
onlyIf: viewModel.onlyIf(),
}
})
};
ko.applyBindings(viewModel);
.is-invalid {
border: thick solid red;
}
.text-danger {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>
<div id="domTest">
<button type="button" data-bind="click: clicked">Submit</button>
<input type="password" class="form-control" id="password" placeholder="Password" data-bind="validationOptions:{ insertMessages: false }, textInput: user.password">
<div class="text-danger error-message" data-bind="validationMessage: user.password" />
</div>