Я использую нокаутную привязку вместе с плагином нокаутной проверки (https://github.com/Knockout-Contrib/Knockout-Validation)
Я хочу показывать сообщения об ошибках только при отправке формы, а не при размытии ввода, есть идеи?
HTML
<div id="formErrorMessages" data-bind="visible: errors().length != 0">
<p data-bind="validationMessage : email"></p>
<p data-bind="validationMessage: firstName"></p>
<span class="arrow-down"></span> </div>
<form id="short-reg-form" method="post" data-bind="validationOptions: { insertMessages:false,decorateInputElement:true,errorElementClass:'input-error' },submit:submit">
<input type="text" name="email" data-bind="value: email" class="email" data-placeholder="Email"/>
<input type="text" name="firstName" data-bind="value: firstName" class="right-input" data-placeholder="First Name"/>
</form>
JS
функция просмотра модели () {
переменная самость = это;
self.showErrorsDiv = ko.observable(false); //Email var emailPlaceholder = getPlaceholder("email"); self.email = ko.observable("") .extend({ ng_required: { params: emailPlaceholder, message: notValidValueReturnString(emailString) }, ng_email: { params:emailPlaceholder, message:notValidValueReturnString(emailString) } }).clearError(); // invoke clearError to prevent validation on page load. //--Email //FirstName var firstNamePlaceholder = getPlaceholder("firstName"); self.firstName = ko.observable("").extend({ ng_required: { params: firstNamePlaceholder, message : notValidValueReturnString(firstNameString) } }); //--FirstName self.submit = function (e) { if (self.errors().length == 0) { // check form validation. alert(true); } else { alert(false); } //prevent form submit default behavior. return false; }; // utilities function getPlaceholder(nameAttributeVal) { return $('#short-reg-form input[name="' + nameAttributeVal + '"]').attr("data-placeholder"); }//--utilities
}
$(function () { // attach view model to the DOM var pageModel = new viewModel(); pageModel.errors = ko.validation.group([pageModel.email,pageModel.firstName]); ko.applyBindings(pageModel);
});
благодаря.