mut*_*tex 7 knockout-validation
Knockout验证插件是否有任何选项可以将错误类应用于错误的输入的父级或父级父级?如果没有,是否有人可以建议修改敲除验证的方法来做到这一点?
请参阅以下小提琴,了解我正在努力实现的目标.开箱即用的敲除验证设置了输入的类,但我希望它改为设置包含控件组的类:
视图
<p>Clear the field and tab out of it to "see" the current behaviour - error class is added directly to the input by knockout validation. But I want to add class "error" to control-group containing the input instead of directly to the input.</p>
<div class="form-horizontal">
<div class="control-group">
<label class="control-label">Field</label>
<div class="controls">
<input type="text" data-bind="value: testField" class="">
</div>
</div>
<button data-bind="click: setErrorOnControlGroup">Click to see desired effect</button>
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
ko.validation.configure({
registerExtenders: true,
messagesOnModified: true,
decorateElement: true,
errorClass: 'error',
insertMessages: false,
parseInputAttributes: true,
messageTemplate: null
});
var viewModel = {
testField: ko.observable("123").extend({required: true}),
setErrorOnControlGroup: function() {
$("input.error").removeClass("error");
$(".control-group").addClass("error");
}
};
ko.applyBindings(viewModel);
Run Code Online (Sandbox Code Playgroud)
样式表(仅限说明 - 我不应该这样,因为我想使用twitter引导程序样式)
/*NOTE: This is not actually the style I want, it just illustrates the default behaviour of knockout validation*/
input.error {
background-color: #aaffaa;
}
Run Code Online (Sandbox Code Playgroud)
我问,因为我使用的是twitter引导程序样式,它使用输入的父控件组元素来设置"错误"输入的样式.