Knockout-Contrib / Knockout-Contrib/Knockout-Validation

Exposing default options

Open
#150 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1k
Forks
366
PR merge metrics
No merged PRs in 30d

Description

It would be really great if Knockout Validation exposes default options so users could change them without providing same parameters to init method all the time.

Currently, I have the following init code on each page with validation:

```
ko.validation.init({
messageTemplate: 'errorTemplate',
errorElementClass: 'input-validation-error',
...
});
```

If default options were exposed, then I could have something like this in common application initialization code:

```
ko.validation.defaults.messageTemplate: 'errorTemplate';
ko.validation.defaults.errorElementClass: 'input-validation-error';
```

Also, we could avoid problems with `init` method being called too early. Since Knockout Validation overrides `ko.applyBindings` to inject `init` call, if you are calling `ko.applyBindings` before explicit `ko.validation.init`, the options you provide with `init` won't be applied (unless you force them with second parameter = `true`).

We had a problem today where ASP.NET MVC partial view containing some KO bindings has been included before the `ko.validation.init` method of the main page. It took me a while to figure out why `init` was ignoring my options override and to force it. Again, it would be a lot easier to change the defaults.

What do you think?

Contributor guide

Open the contributing guide

Research direction

Start by reading the ko.validation.init method and the ko.applyBindings override described in the issue, then trace how options are stored and applied. Done means users can set shared defaults through ko.validation.defaults and those values are respected when bindings initialize, including when applyBindings runs before an explicit init call.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.