knockout-validation dynamic

1,102 views
Skip to first unread message

Lee Atkinson

unread,
Jul 10, 2012, 11:13:01 AM7/10/12
to knock...@googlegroups.com
Is it possible, using the knockout-validation plugin (https://github.com/ericmbarnard/Knockout-Validation) to dynamically change it's values?

For example, is it possible to vary the pattern depending on other values within the object:

var foo = {
isComplicated: true,
name: ko.observable().extend({
pattern: {
params: //somehow determine the regex here by e.g. reading isComplicated
})
}

jean.seba...@gmail.com

unread,
Jul 14, 2012, 7:51:51 PM7/14/12
to knock...@googlegroups.com
I'm about to use the validation plugin too and I have the same question. One thing that comes to mind is to make isComplicated an observable and make pattern an calculated obervable that depends on isComplicated. When is complicated changes, your computed observable would be called.

I haven't tried it myself so I don't know if that would work but it might be worth a try.

Kurt.J...@concencor.com

unread,
Aug 9, 2012, 4:39:09 PM8/9/12
to knock...@googlegroups.com
You can simply add a new validation rule like:

//Allows us to pass an observable regex
ko.validation.rules['ko_pattern'] = {

validator: function (val, regex) {
return ko.validation.utils.isEmptyVal(val) || val.match(regex()) != null;
},
message: 'Please check this value.'
}

FYI regex is an ko observable.
Reply all
Reply to author
Forward
0 new messages