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
Solution 1:
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.
Solution 2:
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"); });
This is caused by how
select
is handled (howoptions binding
is handled I believe) and explains why the validation is done oncountry
.insertMessages: false
will hide all messages, then you need to handle them with thevalidationMessage binding
.
Simply remove the init value of your observable (demo):
self.Code = ko.observable();self.country = ko.observable(); // no ""
Solution 3:
Using this is perfect:
ko.validation.group(yourViewModel, { deep: true }).showAllMessages(false);
However it must be done after calling applyBindings:
ko.applyBindings(yourViewModel);
Otherwise it doesn't work.
Post a Comment for "Knockout Dropdown Validation On Load? Required"