Reputation: 21
have been trying to set up validation like (required, min length, max lenght etc) for the properties on my knockout observables. I created my observables using Mapping plugin. I found the followign links, but could someone help me, figure out which is the right way to go, to set up validation. Here are the links that i was looking at.
http://knockoutjs.com/documentation/extenders.html
I put my code in jsfiddle, Currently it is not running, since it is complaining about knockout files. But Thought some one can see my code here http://jsfiddle.net/wQfuM/13/
Upvotes: 2
Views: 1033
Reputation: 15984
I couldn't get your jsfiddle to work. but here's a simple way to do it. You can control the creation of the observables using the mapping plugins mapping options object.
var json = {
someValue: "foo"
};
var viewModel = function(data) {
ko.mapping.fromJS(data, {
someValue: {
create: function(options) {
return ko.observable(options.data).extend({ minLength: 10 });
}
}
}, this);
};
ko.applyBindings(new viewModel(json));
http://jsfiddle.net/madcapnmckay/b3UrF/1/
Hope this helps.
Upvotes: 2