contrapsych
contrapsych

Reputation: 1949

Can I have form controls in backbone-forms that don't update the model directly?

I have never used backbone.js before, and I am trying to create a form using backbone-forms that, when its commit function is called and it updates the model, it only updates certain fields in the model and/or computes values from form fields to put in different fields of the model.

Here is an example from my code.

queryForm = new Backbone.Form({
        model: queryModel,
        submitButton: "Submit",
        schema: {
            start_date: { type: 'Text', title: "Date" },
            radio_range: { type: "Radio",
                title: "Date Method",
                options: { value1: "Start and End", value2: "Minute Range"}},

            end_date: { type: 'Text', title: "Range (Minutes)" },
            firewall_ip: { type: 'Text', title: "Firewall IP" },
            firewall_port: { type: 'Text', title: "Firewall Port" }
        }

    }).render();

When the user changes to radio to "Minute Range", I want to change the start_date box to just be a date box, and have the end_date box become the range. When the form is committed with the commit function, I still want to use the date and range to create a start and end date that will actually be put in the model, rather than what it does currently, which is put the value of what's in the boxes directly in the model. What would be the best way to accomplish this? Should I override the commit function for this particular class? Or should I not use backbone-forms and just use backbone with templates and events?

Upvotes: 0

Views: 156

Answers (1)

evilcelery
evilcelery

Reputation: 16149

Yes, overriding the form instance's commit() method should work. Otherwise you could override the getValue() method too.

In terms of changing the inputs depending on other values changing, there is a link to a demo for this on the readme.

Upvotes: 1

Related Questions