Extending the product model
To validate our models using the Knockout Validation library, we are going to extend our model's attributes. Extenders are a basic feature of Knockout. Using extenders we can add some properties to our observables to increase their behavior. For more information on extenders, please refer to the following link:
http://knockoutjs.com/documentation/extenders.html
We are going to extend our product model with some properties that will allow us to validate data by following these steps:
Go to the
models/Product.js
file.Update the
name
field. It should have at least three letters and should contain just letters, numbers, and dashes:_name = ko.observable(name).extend({ required: true, minLength: 3, pattern: { message: 'Hey this doesn\'t match my pattern', params: '^[A-Za-z0-9 \-]+$' } })
Update the
price
to allow just numbers, and set a range (maximum and minimum values) for it:_price = ko.observable(price).extend({ required: true, number:true, min...