简体   繁体   English

Knockout验证 - 如何显示单个错误消息

[英]Knockout Validation - How to show a single error message

I check my view model on submit for validation as described here on SO, actually . 确实在提交验证时查看了我的视图模型, 如此处所述

My form has a "Save Progress" action in addition to the "Submit" action. 除“提交”操作外,我的表单还有“保存进度”操作。 It submits to the server in much the same way, but has fewer required fields. 它以相同的方式提交给服务器,但所需的字段较少。

I would like to keep the four absolutely required fields where they currently are in the View Model ... ie keep them in the larger validation group for submission. 我想保留它们目前在View Model中的四个绝对必需的字段...即将它们保存在更大的验证组中以供提交。

Is there a way in Knockout Validation to simply show specific messages in the same way as showAllMessages() would for the full validation group? 在Knockout Validation中是否有一种方法可以像showAllMessages()对完整验证组一样简单地显示特定消息? I have looked through the source, but couldn't find anything like showMessage() attached to a single error. 我查看了源代码,但找不到像单个错误附加的showMessage()

Or, is there a way to pick and choose fields from my View Model and put them in their own validation group (but keep them in the larger group as well)? 或者,有没有办法从我的视图模型中选择字段并将它们放在自己的验证组中(但也将它们保存在更大的组中)?

So, as an example: 所以,作为一个例子:

var ViewModel = ko.validatedObservable({
  requiredForSave1:  ko.observable().extend({ required: true }),
  requiredForSave2:  ko.observable().extend({ required: true }),
  requiredForSubmit: ko.observable().extend({ required: true })
  // ... and many more.
});

$('#sumbit').on('click', function(){

  //check the entire validation group
  if ( ViewModel.errors().length === 0 ){
    doSubmit();
  }
  else{
    ViewModel.errors.showAllMessages();
  }
});

$('#save').on('click', function(){

  //check only part of the validation group
  if ( ViewModel.requiredForSave1.isValid() &&
       ViewModel.requiredForSave2.isValid() ){

    doSubmit();
  }
  else{
     //show only one or two specific validation messages.
     //???
  }

});

Is there a way to fill in that last else block, or should I be taking a different approach to this? 有没有办法填写最后的else块,或者我应该采取不同的方法吗?

Thanks 谢谢

Or, is there a way to pick and choose fields from my View Model and put them in their own validation group (but keep them in the larger group as well)? 或者,有没有办法从我的视图模型中选择字段并将它们放在自己的验证组中(但也将它们保存在更大的组中)?

Yes, you can define as many groups as you want; 是的,您可以根据需要定义任意数量的组; and observables can be in multiple validation groups. 和observables可以在多个验证组中。

So, for example, let's say your validation group for all of the errors in your view model is the following: 因此,例如,假设您的视图模型中的所有错误的验证组如下:

ViewModel.errors = ko.validation.group(ViewModel);

You can also add individual groups like this: 您还可以添加以下各个组:

ViewModel.saveErrors = ko.validation.group([ViewModel.requiredForSave1, ViewModel.requiredForSave2]);

Also, by calling showAllMessages on a validation group you are only showing the messages for the observables within that group. 此外,通过在验证组上调用showAllMessages ,您只显示该组中的可观察对象的消息。 ViewModel.saveErrors.showAllMessages() will only show the validation messages for requiredForSave1 and requiredForSave2 ViewModel.saveErrors.showAllMessages()仅显示requiredForSave1requiredForSave2的验证消息

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM