backbone-forms remove built-in validators in runtime

145 Views Asked by At

I have successfully implemented backbone-forms plug-in with it's validators, for example:

var SampleModel = Backbone.Model.extend({
    schema: {
        field1: {
            title: $t.field1, validators: ['required', 'number']
        },
        field2: {
            title: $t.field2, type: 'Select', options: $options.field2, validators: ['required']
        },
        notes: {
            title: $t.notes
        }
    }
});

Now I am trying to find "right" (at this moment - any) way to disable built-in validators, on, for example, some check box click. After checkbox is clicked, allow form to be saved without validation.

I tried to rebuild this.model.schema for each field without validators and after did this.model.form.commit(), but it did nothing.

Can you, please, give some advice?

EDIT:

At now, I am using "dirty" method adding additional argument into commit method. See Backbone-forms commit method source:

commit: function(options, dontValidate) {
    //Validate
    options = options || {};

    var validateOptions = {
        skipModelValidate: !options.validate
    };

    // DIRTY 
    if(!dontValidate) {
        var errors = this.validate(validateOptions);
        if (errors) return errors;
    }

    //Commit
    var modelError;

    var setOptions = _.extend({
        error: function(model, e) {
            modelError = e;
        }
    }, options);

    this.model.set(this.getValue(), setOptions);

    if (modelError) return modelError;
},
0

There are 0 best solutions below