thedersen / backbone.validation

A validation plugin for Backbone.js that validates both your model as well as form input
thedersen.com/projects/backbone-validation
MIT License
1.32k stars 300 forks source link

New feature: call one validation rule from another #325

Closed denar90 closed 8 years ago

denar90 commented 8 years ago

Hi. I propose to add one property to isValid method to add possibility pass computed value. It will be helpful when using data binding. For example I have 2 validation rules for 2 fields: 'name' , 'email';

Backbone.Model.extend({
    validation: {
        name: {
            nameRule: true
        },
        email: {
            emailRule: true
        }
    }
});

And I want to call nameRule form emailRule

        nameRule: function(value, attr, customValue, model, computed) {
            if (value.length === 0) return 'Error';
        },

        emailRule: function(value, attr, customValue, model, computed) {
            this.model.isValid('name');
        }

In this case computed.email value in nameRule will be old and it will be not equal to real email. If pass 2 arguments (option, computed) into isValid method (this.model.isValid('name', computed)), we can use computed value instead of this.attributes in isValid. If computed was not passed we can use this.attributes @thedersen What do you think about it? Is it worthwhile?

denar90 commented 8 years ago

Issue closed because of https://github.com/thedersen/backbone.validation#can-i-call-one-of-the-built-in-validators-from-a-custom-validator