amcardwell's avatar

Dynamic Form Generator tips?

Hey guys. Can anyone give me some advice on the best way to use laravel to generate dynamic forms? Currently I am using a many-to-many relationship... but I KNOW there has to be a better, faster, and cleaner way... Here is my current model structure:

Form.php
- id
- name (string)

FormAttribute.php
- id
- form_id
- field_id
- attr_name (string)
- attr_type (int)       // Boolean, String, Integer, etc.
- attr_required (bool)

FormField.php
- id
- attribute_id
- value (text)

I dont have that many forms yet (< 5), but for flexibility's sake I know this needs to be improved. I would also like to avoid using JSON. I tried the suggestion of using MongoDB, but I could not figure out how to use it. I'm currently using MySQL on Laravel 5.2. Any tips/pointers?

0 likes
2 replies
jekinney's avatar

I have set it up as follows:

A table with inputs as a string. The actual form is saved as a string in forms table

I used drag and drop in the ui to build the form. Allowed user to name each input and set data type (integer, text, date, etc...)

When saving it creates a database migration via commands, migrats the new table to receive the forms data. The forms HTML id built like I said as a string (could do json). The action URL is also set by forms slug of name.

Controllers post method accepts slug then creates new row of data on post of the dynamic form.

Now the rest or changes depends on use case.

1 like
amcardwell's avatar

hmm... okay that makes things a bit clearer. the only thing is... I hate the fact that I would have to generate a new table for every new form... Also, do you also generate an associated model for each new table? How else do you make the columns fillable and set relations?

Please or to participate in this conversation.