简体   繁体   English

剔除下拉验证负载? 需要

[英]knockout dropdown validation on load? required

There are many post on this one which i find not helpful and i need to find out why and how . 关于这一篇文章有​​很多文章,我认为这些文章无济于事,我需要找出原因和方式。

I made a fiddle with two controls dropdown and textbox and applied required validation with .extend My fiddle 我做了两个控件下拉列表和文本框小提琴,而应用需要验证.extend 我的小提琴

Question 1 : when i first load my page i get a validation error beside dropdown but not beside textbox ? 问题1:当我第一次加载页面时,在下拉列表旁边但在文本框旁边却收到验证错误? i am confused why is that . 我很困惑为什么。

Question 2 : Ok . 问题2:好的。 its awkward to display a error message on load so i planned to disable error messages display on load . 它在加载时显示错误消息很尴尬,所以我计划禁用在加载时显示错误消息。 Added this line ko.validation.init({ insertMessages: false }); 添加了这一行ko.validation.init({ insertMessages: false }); . By this i got rid of on load error message display . 这样,我就摆脱了加载错误消息显示。 BUT when i click on submit i can't find the error message text beside textbox or dropdwon . 但是当我点击提交我无法找到文本框或dropdwon旁边的错误消息文本。

One way or the other dropdown is failing my cause to display error text. 一种方式或另一种方式使我无法显示错误文本。

Any feasible approach is much appreciated . 任何可行的方法都值得赞赏。

To disable messages on load you need this instruction 要禁用加载消息,您需要此说明

self.Errors.showAllMessages(false)

this will disable all message on first load. 这将在首次加载时禁用所有消息。 setting this will not help you 设置此设置对您没有帮助

ko.validation.init({ insertMessages: false })

For your first problem, You must be doing this 对于第一个问题,您必须这样做

<select data-bind='blah,validationElement:TextBoxField' >

this will cause it. 这将导致它。 Hope it helps. 希望能帮助到你。

  1. If you subscribe to your observables, you will see that country is actually set on load by ko, which is not the case for code ( demo ): 如果你订阅你可观察,你会看到, country实际上是由KO,这对于代码的情况下(在负载设置演示 ):

     self.Code.subscribe(function () { alert("Codes changed"); }); self.country.subscribe(function () { alert("Country has changed"); }); 

    This is caused by how select is handled (how options binding is handled I believe) and explains why the validation is done on country . 这是由select的处理方式(我相信options binding的处理方式)引起的,并解释了为什么对country进行验证。

  2. insertMessages: false will hide all messages, then you need to handle them with the validationMessage binding . insertMessages: false将隐藏所有消息,然后您需要使用validationMessage binding来处理它们。

Simply remove the init value of your observable ( demo ): 只需删除可观察对象( demo )的初始化值:

self.Code = ko.observable();
self.country = ko.observable(); // no ""

Using this is perfect: 使用它是完美的:

ko.validation.group(yourViewModel, { deep: true }).showAllMessages(false);

However it must be done after calling applyBindings: 但是,必须调用applyBindings 之后完成:

ko.applyBindings(yourViewModel);

Otherwise it doesn't work. 否则它将无法正常工作。

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

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