You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes you will have a viewmodel property that is only required in depending on some other circumstances. You can use the onlyIf parameter, which accepts a function, to specify when that field is required. The required validator will only be applied if the onlyIf function returns true.
For example, consider an address block that requires a state only if the country is "US".
ko.validation.init();functionAddress(){varself=this;self.availableCountries=['AU','NZ','US'];self.availableStates=['Alabama','Alaska','Arizona','Arkansas'];self.country=ko.observable().extend({required: true});//State is only required if 'US' is selected as countryself.state=ko.observable().extend({required: {onlyIf: function(){returnself.country()==='US';}}});};varaddress=ko.validatedObservable(newAddress())();ko.applyBindings(address);