Knockout.js 淘汰验证在用户输入任何数据之前自动显示错误消息
我正在尝试实现敲除验证和敲除映射。我有一个ASP.Net MVC WebAPI,它将JSON数据发送到我的客户机,在那里我将JSON映射到模型中的可观察对象: 视图模型:Knockout.js 淘汰验证在用户输入任何数据之前自动显示错误消息,knockout.js,knockout-2.0,knockout-mapping-plugin,knockout-validation,Knockout.js,Knockout 2.0,Knockout Mapping Plugin,Knockout Validation,我正在尝试实现敲除验证和敲除映射。我有一个ASP.Net MVC WebAPI,它将JSON数据发送到我的客户机,在那里我将JSON映射到模型中的可观察对象: 视图模型: ko.validation.rules.pattern.message = 'Invalid.'; ko.validation.configure({ registerExtenders: true, messagesOnModified: true, insertMessages: true,
ko.validation.rules.pattern.message = 'Invalid.';
ko.validation.configure({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
parseInputAttributes: true,
messageTemplate: null,
decorateElement: true,
errorClass: 'error'
});
var MakePaymentViewModel = function ()
{
var mapDataFromJson = function (jsonFromServer)
{
var model = ko.mapping.fromJS(jsonFromServer);
self.PaymentAmount(model.PaymentAmount());
self.MakePaymentTo(model.MakePaymentTo());
self.MakePaymentOn(model.MakePaymentOn());
};
var self = this;
/* MAKE PAYMENT MODEL*/
self.PaymentAmount = ko.observable().extend({ required: { message: 'required'}});
self.MakePaymentTo = ko.observable().extend({ required: { message: 'required'}});
self.MakePaymentOn = ko.observable().extend({ required: { message: 'required' }});
self.Errors = ko.validation.group(self);
self.MapDataFromServer = function (jsonFromServer)
{
mapDataFromJson(jsonFromServer);
};
self.DoPaymentConfirmation = function ()
{
console.log('error count: ' + self.Errors().length);
};
};
<form id="MakePaymentForm" autocomplete="off">
<div class="input-wrapper">
<label for="SendingAmount" class="text-left">I am sending <small>*</small></label>
<input id="PaymentAmount" type="text" data-bind="value: PaymentAmount"/>
</div>
<div class="input-wrapper">
<label for="MakePaymentTo" class="text-left">to <small>*</small></label>
<input id="MakePaymentTo" type="text" data-bind="value: MakePaymentTo"/>
</div>
<div class="input-wrapper">
<label for="MakePaymentOn" class="text-left">on <small>*</small></label>
<input name="MakePaymentOn" id="MakePaymentOn" type="text" data-bind="value: MakePaymentOn"/>
</div>
<button type="submit" class="small radius" data-bind="click: DoPaymentConfirmation">Send</button>
</form>
以下是我的观点:
ko.validation.rules.pattern.message = 'Invalid.';
ko.validation.configure({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
parseInputAttributes: true,
messageTemplate: null,
decorateElement: true,
errorClass: 'error'
});
var MakePaymentViewModel = function ()
{
var mapDataFromJson = function (jsonFromServer)
{
var model = ko.mapping.fromJS(jsonFromServer);
self.PaymentAmount(model.PaymentAmount());
self.MakePaymentTo(model.MakePaymentTo());
self.MakePaymentOn(model.MakePaymentOn());
};
var self = this;
/* MAKE PAYMENT MODEL*/
self.PaymentAmount = ko.observable().extend({ required: { message: 'required'}});
self.MakePaymentTo = ko.observable().extend({ required: { message: 'required'}});
self.MakePaymentOn = ko.observable().extend({ required: { message: 'required' }});
self.Errors = ko.validation.group(self);
self.MapDataFromServer = function (jsonFromServer)
{
mapDataFromJson(jsonFromServer);
};
self.DoPaymentConfirmation = function ()
{
console.log('error count: ' + self.Errors().length);
};
};
<form id="MakePaymentForm" autocomplete="off">
<div class="input-wrapper">
<label for="SendingAmount" class="text-left">I am sending <small>*</small></label>
<input id="PaymentAmount" type="text" data-bind="value: PaymentAmount"/>
</div>
<div class="input-wrapper">
<label for="MakePaymentTo" class="text-left">to <small>*</small></label>
<input id="MakePaymentTo" type="text" data-bind="value: MakePaymentTo"/>
</div>
<div class="input-wrapper">
<label for="MakePaymentOn" class="text-left">on <small>*</small></label>
<input name="MakePaymentOn" id="MakePaymentOn" type="text" data-bind="value: MakePaymentOn"/>
</div>
<button type="submit" class="small radius" data-bind="click: DoPaymentConfirmation">Send</button>
</form>
因此,mapDataFromJson函数用空数据填充可观察对象并触发验证规则,因此用户界面在用户输入任何数据之前立即显示错误消息
有没有办法解决这个问题?我是新手,所以我可能做得不对。谢谢你抽出时间。谢谢杰米为我指出了正确的方向。以下是我为解决问题所做的:
var mapDataFromJson = function (jsonFromServer)
{
var model = ko.mapping.fromJS(jsonFromServer);
if (model.PaymentAmount() != null)
{
self.PaymentAmount(model.PaymentAmount());
}
if (model.MakePaymentTo() != null)
{
self.MakePaymentTo(model.MakePaymentTo());
}
if (model.MakePaymentOn() != null)
{
self.MakePaymentOn(model.MakePaymentOn());
}
};
然后在绑定到submit按钮的函数中,我执行以下操作:
self.DoPaymentConfirmation = function ()
{
if (self.Errors().length == 0)
{
//POST TO SERVER
}
else
{
self.Errors.showAllMessages();
}
};
你能在
mapDataFromJson
内的空引用检查中包装可观测值的设置吗?我尝试过,验证不会立即显示,但是,当我按下提交按钮时,我可以控制台.log('error count:'+self.Errors().length)查看是否有错误,但现在UI中没有显示任何错误消息。一定是相反的一天,我想出来了。相反的一天是上周二。我忘记使用self.Errors.showAllMessages();它正在按预期工作。谢谢你的帮助。@TomSchreck你能把它写在一个答案中,并把它标记为已回答,这样其他人就可以看到它被解决了吗