剔除下拉列表负载验证?必需的 [英] 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 .
我用两个控件下拉菜单和文本框进行了提琴演奏,并使用.extend
我的提琴
I made a fiddle with two controls dropdown and textbox and applied required validation with .extend
My fiddle
问题1::当我第一次加载页面时,在下拉菜单旁边但在文本框旁边却收到验证错误?我很困惑为什么.
Question 1 : when i first load my page i get a validation error beside dropdown but not beside textbox ? i am confused why is that .
问题2:好的.它在加载时显示错误消息很尴尬,所以我计划禁用在加载时显示错误消息.添加了此行ko.validation.init({ insertMessages: false });
.这样一来,我就摆脱了加载错误消息的显示.但是,当我单击提交"时,我无法在文本框或dropdwon旁边找到错误消息文本.
Question 2 : Ok . 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 });
. 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 .
一种方式或其他方式使我无法显示错误文本.
One way or the other dropdown is failing my cause to display error text.
任何可行的方法都值得赞赏.
Any feasible approach is much appreciated .
推荐答案
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):
self.Code.subscribe(function () { alert("Codes changed"); });
self.country.subscribe(function () { alert("Country has changed"); });
这是由select
的处理方式(我相信options binding
的处理方式)引起的,并解释了为什么在country
上进行验证.
This is caused by how select
is handled (how options binding
is handled I believe) and explains why the validation is done on country
.
insertMessages: false
将隐藏所有消息,然后您需要使用validationMessage binding
处理它们.
insertMessages: false
will hide all messages, then you need to handle them with the validationMessage binding
.
只需删除可观察对象的初始化值(演示):
Simply remove the init value of your observable (demo):
self.Code = ko.observable();
self.country = ko.observable(); // no ""
这篇关于剔除下拉列表负载验证?必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!