powmedia / backbone-forms

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

How to set value for SelectBox options #296

Closed hegdeashwin closed 11 years ago

hegdeashwin commented 11 years ago

I am not able to find any example or documentation regarding how to set values for select box options. Yes! options property sets options text but how to set values for respected options text. like E.g. <option value="1">option1</option>

var form = new Backbone.Form({
    schema: {
        /* Select Box */
        SelectBox: {
            type:'Select',
            options: [
                'Option Text 1',
                'Option Text 2',
                'Option Text 3',
                'Option Text 4'
            ]
        }
    },
    data: {
        SelectBox: 'Option Text 3'
    }
}).render();

$('#form-container').append(form.el);
powmedia commented 11 years ago

There's a few ways to populate - see the Select docs:

So in your case you can do:

var form = new Backbone.Form({
    schema: {
        /* Select Box */
        SelectBox: {
            type:'Select',
            options: [
                { val: 1, label: 'Option Text 1' },
                { val: 2, label: 'Option Text 2' }
            ]
        }
    },
    data: {
        SelectBox: 'Option Text 3'
    }
}).render();

$('#form-container').append(form.el);
hegdeashwin commented 11 years ago

ok; i got that. Thanks

szymdzum commented 10 years ago

Thanks as well. What if I want to add

<option selected="selected">3</option>
powmedia commented 10 years ago

You'd set that as the form data, for example:

var form = new Backbone.Form({
  schema: {
    foo: { type: 'Select', options: [1,2,3,4] }
  },
  data: {
    foo: 3
  });

Or if you're using a model, set the 'foo' attribute to 3