I built a form builder with which administrators of the system I work on can build forms (or blueprints as we call them). The structure of these blueprints are as follows:
- blueprint X
- section 1
- question 1 (Text)
- question 2 (LongText)
- question 3 (File)
- section 2
- question 1 (Text)
- question 2 (Dropdown)
- section 3
- question 1 (Signature)
- section 1
These blueprints can then be instantiated by users in the application (we call that a (filled in) 'form'), after which the answers get stored in the database. That storage is very simple:
(filled in) forms
- blueprint_id
answers
- form_id
- question_id
- answer
So every form has a set of question => answer pairs. When a form is opened, the blueprint questions are rendered and their respective answers are loaded from the database.
The problem I face now is that new functionality requires a section to be repeated N times. That N is external input and could be anywhere between 1 and 20. The repeated rendering applies only to certain sections, so the blueprint above could look like this when N = 2.
- blueprint X
- section 1
- question 1 (Text)
- question 2 (LongText)
- question 3 (File)
- section 2 (N1)
- question 1 (Text)
- question 2 (Dropdown)
- section 2 (N2)
- question 1 (Text)
- question 2 (Dropdown)
- section 3
- question 1 (Signature)
- section 1
That adds a whole layer of complexity to the storage of answers, because for questions 2.1 and 2.2 there will be 2 separate answers instead of 1.
The solution I'm working on now would just cumulate these answers into JSON objects, where an answer record would look like this:
question_id answer
1 ["first answer","second answer"]
Or a little more verbose:
question_id answer
1 {"N1":"first answer","N2":"second answer"}
Where N1 and N2 are section identifiers.
Would this be a valid way of storing this? Are there other ways?
1 Answer 1
I found another way. Instead of rendering the section twice, I will just generate a new blueprint with the section configured twice. This means that I won't have to deal with multiple answers on the same question.