Skip Validation Of Model Attribute Using Backbone.validation
I have a view that is rendered dynamically. It may have some inputs or may not have. After a user fills everything and tries to send data I call this.model.isValid(true) ( or this.
Solution 1:
How can I allow empty values but still validate if the user enters something?
By default, if you configure a validator for an attribute, it is considered required. However, if you want to allow empty values and still validate when something is entered, add
required: false
in addition to other validators.
validation: { value: { min:1, required:false } }
If you can't let empty values (like at creation), Backbone.validation overrides isValid
adding features to the default behavior. What's interesting is the array parameter we can pass:
// Check if name and age are validvar isValid = model.isValid(['name', 'age']);
With this, we can then validate only the fields that exist within the model at the moment:
model.isValid(_.keys(model.attributes));
Post a Comment for "Skip Validation Of Model Attribute Using Backbone.validation"