team-formalist / formalist-rb

Flexible form builder
MIT License
22 stars 4 forks source link

Allow composition of forms #7

Open timriley opened 8 years ago

timriley commented 8 years ago

To allow common form "snippets" to be reused consistently across a site, we should allow them to be composed onto other forms. Something like this:

class MyForm < Formalist::Form
  # Normal form definition
  field :title, type: :text

  # Compose another form object and use it for all the data inside the `venue` key
  attr :venue, form: :venue_details
end

my_form = MyForm.new(venue_details: another_form_object)

Using symbols as the external form references here allows us to pass them in as fully configured form instances when the form is created. It also means we can use them at any depth within the form definition.

Once we add support for validation, etc., we'll need to make sure all of that info merges into the main form nicely too.

From the example above, it's pretty clear how we could use external forms on attr and many definitions. What's not so clear is how you could just "inline" an external form at any arbitrary point. Maybe we need to add an import :some_other_form definition?