powmedia / backbone-forms

Form framework for BackboneJS with nested forms, editable lists and validation
MIT License
2.17k stars 413 forks source link

Passing id attribute to options hash does not set form element id #337

Open adriano-di-giovanni opened 10 years ago

adriano-di-giovanni commented 10 years ago

Despite Backbone.Form is a Backbone.View, I can't get the form id attribute set if I pass the id attribute to the options hash.

var
  AddEditTraineeForm = Backbone.Form.extend({
    id: 'addEditTraineeForm'
  });

Thanks for backbone-forms, Adriano

adriano-di-giovanni commented 10 years ago

I post my solution. I think it can be useful to someone else.

First, override Backbone.Form.template

Backbone.Form.template = _.template('\
  <form data-fieldsets <% if (id) { %>id="<%= id %>"<% } %>></form>\
');

Second, use templateData attribute in the options hash

var
  AddEditTraineeForm = Backbone.Form.extend({
    templateData: {
      id: 'addEditTraineeForm'
    }
  });