backbone-forms icon indicating copy to clipboard operation
backbone-forms copied to clipboard

Dynamically insert or remove fields

Open wyuenho opened this issue 12 years ago • 8 comments

Hi! Thanks for this library!

I know you can dynamically insert select options based on changes of another field value, but is there a way to dynamically insert or remove a field based on field value?

Thanks in advance for answering.

wyuenho avatar Jul 24 '13 20:07 wyuenho

Are they always the same ? if so maybe generate it as a hidden field and then change it to the field type you want ?

exussum12 avatar Jul 24 '13 21:07 exussum12

They are always the same type, but not always have a value. It's be nice to be able to insert or remove the fields entirely so I don't have to conditionally filter out empty fields on Model.save.

wyuenho avatar Jul 24 '13 22:07 wyuenho

Yes - you can just change schema however you want. Your model's schema function could generate a different schema depending on other attributes. Or you could pass a different schema when creating a form.

You could even also overwrite an existing form's schema property (though since this isn't nicely abstracted currently you may also have to re-set form.fieldsets as well) and re-render

philfreo avatar Jul 24 '13 22:07 philfreo

Created #274 which may make this easier in some cases where you want to change/re-render an existing form.

philfreo avatar Jul 24 '13 23:07 philfreo

I'd really prefer it if I could insert or remove fields with methods, which essentially calls createField internally and append or remove from the container. Rerendering is a no go on mobile. I don't thinks people would want the screen to be jumping around, losing focus to field but the keyboard is still hanging around etc as soon as I've picked a value from a select box.

The way I do it currently is to override the Form#render method and #initialize to listen to couple of key change events, the resulting code is quite nasty.

Makes sense - I didn't realize you meant adding fields while the form was currently visible.

philfreo avatar Jul 24 '13 23:07 philfreo

A simpler workaround in the meantime might be to have a second, additional, form generated after the option from the Select is chosen.

philfreo avatar Jul 24 '13 23:07 philfreo

This is even worse then extending Backbone.Form haha, I'll have to grub thru the fields objects and the DOM elements from the newly created form, and then somehow magically insert them back to my form...

wyuenho avatar Jul 25 '13 00:07 wyuenho

Is this still a work in progress? Was there a work around discovered?

mrowles avatar Oct 28 '14 04:10 mrowles