backbone-forms
backbone-forms copied to clipboard
Dynamically insert or remove fields
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.
Are they always the same ? if so maybe generate it as a hidden field and then change it to the field type you want ?
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.
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
Created #274 which may make this easier in some cases where you want to change/re-render an existing form.
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.
A simpler workaround in the meantime might be to have a second, additional, form generated after the option from the Select is chosen.
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...
Is this still a work in progress? Was there a work around discovered?