Reputation: 590
If I extend a knockout observable like so
var x = ko.observable().
extend({
pattern : {
params: someRegex,
message: "An error"
}
})
.extend({
pattern : {
params: someMoreRegex,
message: "Another error"
}
})
Is this a valid extension for a knockout observable (i.e. multiple pattern extensions)?
The regex for the second pattern is not being validated at all. In some cases it does get triggered but shows the first patterns error message. I have recently upgraded form 1.0.2 to 2.0.3 knockout validation and this has since broken but cannot seem to put a finger on why this is no longer working.
Upvotes: 4
Views: 3169
Reputation: 23382
From this (admitedly, quite old) Github issue, I concluded that this isn't supported by the validation library...
A quick fix could be to create anonymous custom rules that borrow the validator
method from the pattern
extension.
An example (which doesn't make sense, but shows how you can combine two patterns with their own errors):
this.name = ko.observable("").extend({
validation: [{
validator: ko.validation.rules['pattern'].validator,
message: "Must be lowercase",
params: /^[a-z]+$/
}, {
validator: ko.validation.rules['pattern'].validator,
message: "Must be uppercase",
params: /^[A-Z]+$/
}
]
});
You could maybe clean this code up a bit by creating a factory method that returns the required objects, or create a custom rule that takes an array of regular expressions and an array of error messages.
Upvotes: 7