Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nolros's avatar
Level 23

Forms DB structure / relations

I would appreciate some thoughts on how best to structure a frontend forms DB (using angular so building these dynamicically with custom directives). I'm sure others more experienced have done this so would appreciate some experience applied to this problem. My current thinking is 7 tables

  1. forms (form types e.g. registration form) $table->increments('id'); $table->string('name',50);

  2. form fields (text, email, etc) $table->increments('id'); $table->string('name',100); $table->string('label',50); $table->string('type',20); $table->string('placeholder',100);

  3. form validation types (required, autocomplete, min, max, etc.) $table->increments('id'); $table->string('key',100);

  4. form validation values (validation values e.g. max = 100) $table->increments('id'); $table->string(value,100);

  5. form_form_field_pivot (pivot table ) form to field relationship. Multiple forms can have multiple fields e.g. register and subscribe both would have email field

  6. form fields to form validation types pivot (pivot table ) form field fields to field validation types. e.g. text field to email, required , autocomplete, maxlength, etc?

  7. form validation types _to form_form_field_pivot pivot (pivot table ) form field validation to field validation values. Multiple validations could have multiple values e.g. text for one field could be maxlength 30 and another text maxlength could be 100

QUESTIONS:

  1. does the structure make sense? can I simplify complex?
  2. I end up with 3 pivot tables how and struggling with the relationships and how I would get this into a single DB query
0 likes
2 replies
willvincent's avatar

If you're building these dynamically on the front end.. probably the easiest option would be to use a json field or two, and stick to just a single table for form definitions, and another (if needed) for form submissions.

nolros's avatar
Level 23

@willvincent thanks for feedback. Current running older SQL so not json but could place it in a text field per your suggestion. Issue is I feel I would end up with a large amount of parsing to process definitions. Today I have them is JS service classes but it makes for ugly JS / nested JSON and service classes, example, below would be for email. So I end up with large JS files which works but would like to move it to DB to remove bloated JS. I'm sure there is a more streamlined approach using DB. Will attempt your approach to see if I can come up with something simple. Will keep you posted.

'''

        service.getRegisterData = function () {

        return  [
            {
                id: '11',
                name: 'email',
                cssClass: 'form--input',
                label: 'Enter your email',
                type: 'email',
                maxlength: 50,
                minlength: 4,
                placeholder: 'Example: person@msn,com',
                required: true,
                autocomplete: 'on',
                helpTitle: 'Enter your email address',
                helpDescription: 'Enter a valid email address. Ideally something with an @ and a . something :).',
                comparedTo: null,
                messages: [
                    {
                        id: '1111',
                        type: 'email',
                        message: 'Email must be a valid email address'
                    },
                    {
                        id: '1122',
                        type: 'minlength',
                        message: 'Your email address is too short'
                    },
                    {
                        id: '1133',
                        type: 'maxlength',
                        message: 'Your email address is too long'
                    },
                    {
                        id: '1144',
                        type: 'required',
                        message: 'Your email address is required'
                    }
                ]
            }
        ]
    } 

'''

Please or to participate in this conversation.