Knockout JS - How to update KO binding attributes after a page renders? - Needed for JQuery unobtrusive validation

StackOverflow https://stackoverflow.com/questions/21464353

Question

After a page is rendered a field that was set to be required with KO to "data-value=true" is, after user input, not needed to be validated by JQuery unobtrusive validation. How can the attribute be updated to "false" so that the field will not be set to required?

I have tried unsuccessfully using f.attr("data-val", "false"), which updates the Object itself and not attr: item.

Was it helpful?

Solution 2

The solution was to reparse the form.

The important lines of code are below.

currForm.removeData("validator");
currForm.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(currForm);

The solution was found here. jquery.validate.unobtrusive not working with dynamic injected elements

OTHER TIPS

Try using the knockout "attr" binding. http://knockoutjs.com/documentation/attr-binding.html

You could possible pass to the attr binding a KO observable that returns true / false. You could also create a custom KO computed observable which returns true / false based on other propeties on your view model (http://knockoutjs.com/documentation/computedObservables.html)

If you get stuck, post a 'cut back' sample of what you're trying to achieve, including your html and viewmodel javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top