Yii2 Form Builder: Drag Fields, Get a Database Table

yii2-forms is a Yii2 module that builds forms in the browser and stores every submission in its own database table. You drag fields onto a canvas, save, and the module runs the schema change for you.
The interactive demo that used to sit under /yii2-extensions/yii2-forms is retired. The code is still public: github.com/pceuropa/yii2-forms and the Yii extension page.
What It Does
- Form builder with drag and drop sorting, editing and deleting of fields
- CRUD over AJAX, so building a form never reloads the page
- Schema follows the form: creating a form creates a table, adding a field adds a column, renaming a field renames the column
- Render widget that validates submissions through a dynamic model
- Optional RBAC, so only the owner can edit a form
Install
composer require pceuropa/yii2-forms
php yii migrate/up --migrationPath=@vendor/pceuropa/yii2-forms/migrations
Then register the module in your application config:
'modules' => [
'forms' => [
'class' => 'pceuropa\forms\Module',
'formsTable' => '{{%forms}}',
'formDataTable' => 'form_',
'emailSender' => 'info@example.com',
],
],
The builder lives at /forms/module/create, the list of forms at /forms/module/index.
Render a Form
Point the widget at a stored form by id:
use pceuropa\forms\Form;
echo Form::widget(['formId' => 1]);
You can also pass the JSON body directly, which is useful for a landing page that ships with the repository instead of the database.
The Tradeoff
The module writes DDL at runtime. That is the whole idea, and it is also the main risk. Two things follow:
- The database user needs
CREATE TABLEandALTER TABLErights in production. - Your schema stops being fully described by migration files.
For marketing forms, surveys and internal questionnaires, that trade is usually worth it. For data that other parts of the application read and join, write a normal model and a normal migration instead.
State of the Package
The last release is 3.0.4 from December 2019, MIT licensed, requiring PHP 7.0 and Yii 2.0. It runs on jQuery and Bootstrap 3 markup, so on a modern Yii2 project expect to touch the assets before the builder looks right.
Treat the repository as a working starting point rather than a maintained dependency: fork it, pin it, and read the migration before running it against a production database.
Need a form workflow reviewed or ported to a current stack? Write to info@pceuropa.net