winzik/docs/ide/symfony-forms.md
David Nurdin e796498ed1 IDE: Symfony Forms window, a visual form builder for FormTypes
- The Symfony Forms window (projects requiring symfony/form): the project's FormTypes, New form type, twig.yaml's form themes.
- The form builder: fields added from Symfony's types and the project's own (sub-forms), reordered by dragging, removed; each
  type's options edited by kind (label, required, help, attr / row_attr, validator constraints with their options, choices,
  EntityType class / query_builder, CollectionType entry_type / allow_add / allow_delete / by_reference / prototype, RepeatedType,
  date widgets / html5...), configureOptions, form events (listener methods made), data transformers (classes made).
- Add fields from the class: an entity's / DTO's properties with suggested types (Doctrine columns, relations -> EntityType /
  CollectionType, enums, PHP types, names).
- Preview without PHP: the HTML of form_div_layout, form_table_layout, bootstrap_5_layout, tailwind_2_layout with Symfony's ids /
  names, sub-forms, collection prototypes and "Show errors", in the project's base.html.twig with its CSS.
- Round trip: FormTypes read back with php-parser (add() chains, option arrays, conditionals / closures kept locked) and written
  by text edits at php-parser's positions: only what changed, the rest byte for byte; new FormTypes as maker-bundle writes them.
- Generate the controller action (createForm, handleRequest, persist / flush or DTO, flash, redirect) into an existing or new
  controller and its Twig template; every change shown as a diff first.
- ide/src/Symfony/Form*.php + Imports.php (forms.* commands), ide/ui/js/symfony-forms*.js, css/symfony-forms.css; 14 languages.
- Tests: SymfonyFormsTest (exact generated code, parsing, byte-identical round trips, edits, suggestions, generators),
  symfony-forms-preview.test.mjs (HTML per theme), games/ide/tools/ui-forms-check.sh (screenshots); checked on a Symfony 8.1
  project (debug:form, GET renders the fields, invalid POST 422 with the message, valid POST 303 + flash).
- Docs: docs/ide/symfony-forms.md (+ fr), docs/ide.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JkYTqsvYu6AYr9yQ3Q4Nt6
2026-11-16 00:35:45 +01:00

59 lines
4 KiB
Markdown

# Symfony forms
The **Symfony Forms** window (right tool bar, or Tools > Symfony Forms) lists the project's FormTypes (`src/Form/*Type.php`) and its
form themes. It appears in a project whose `composer.json` requires `symfony/form` (or that has `src/Form/`). Click a form type, or
use **Open the Form Builder on This File** on the FormType open in the editor, or **New form type…**: the **form builder** opens over
the workbench.
![The form builder: the fields, a field's options, the preview](images/symfony-forms.png)
## Fields
- **+** adds a field: Symfony's types grouped like its reference (text, numbers, choices, dates, other, buttons) and the project's own
form types (sub-forms). Drag a field (or ↑ ↓) to reorder, the bin removes it.
- **Add fields from the class…** reads the form's data class (an entity or a DTO) and proposes its properties, each with the type it
suggests: from the Doctrine mapping (`text` → TextareaType, `decimal` → MoneyType for a price, dates → DateType / DateTimeType,
`enumType` → EnumType, ManyToOne → EntityType, ManyToMany → EntityType `multiple`, OneToMany → CollectionType), from the PHP type
(bool → CheckboxType, an enum → EnumType) and the name (email, password, url, phone, color). Nullable columns get `required: false`;
the identifier and the timestamps come unchecked.
## Options
The middle column edits the selected field: its name, its type, then the options of that type by kind: label (or none), required,
help, `attr`, `row_attr`, **constraints** (Symfony's validator constraints, each with its own options), `choices`, `expanded`,
`multiple`, `placeholder`, an EntityType's `class` and `query_builder`, a CollectionType's `entry_type`, `entry_options`, `allow_add`,
`allow_delete`, `by_reference`, `prototype`, a RepeatedType's `type` and first / second options, dates' `widget`, `input`, `html5`.
Other options are shown and edited as PHP code.
- **Form** (configureOptions): `data_class`, `validation_groups`, `csrf_protection`, `csrf_token_id`, `allow_extra_fields`,
`translation_domain`, `attr`.
- **Events**: `FormEvents::PRE_SET_DATA`, `POST_SUBMIT`… with their listener method, made in the class when it lacks it.
- **Data transformers**: model or view transformers of a field; their class is made in `src/` when it does not exist.
## Preview
The right column shows the form as the chosen theme renders it, without running PHP: Symfony's default theme, the table theme,
Bootstrap 5 or Tailwind 2, inside the project's own page (`base.html.twig` and its stylesheets). **Show errors** shows the error
states, with the message of each field's first constraint. **Theme** chooses the project's themes (twig.yaml) or one for this form.
![The preview with Bootstrap 5 and the errors shown](images/symfony-forms-errors.png)
## Save and generate
- **Save…** shows a diff, then writes the FormType: only what changed is written where it is (one option, a line added or removed,
the fields moved); comments and the rest of the file are kept as they were. What the builder cannot edit (an `add()` inside an `if`,
a closure listener, a `CallbackTransformer`) is shown locked and never changed.
![Save: only what changed is written](images/symfony-forms-diff.png)
- **Generate controller and template…** makes the action that shows and handles the form (`createForm`, `handleRequest`, valid:
`persist` / `flush` for an entity, or the DTO used, a flash message, a redirection), in an existing controller or a new one, and its
Twig template (`form_start`, a `form_row()` per field or `form_widget(form)`, `form_end`), shown as diffs.
- **Change the themes…** sets `form_themes` in `config/packages/twig.yaml`.
![Generate: the controller and its template, as diffs](images/symfony-forms-generate.png)
## Limits
- A field moved to another `$builder` statement stays in its own statement (the order is kept inside each add() chain).
- Removing an event or a transformer leaves its method or class in place.
- The preview knows Symfony's themes, not a project's own theme blocks; EntityType choices and enum labels are samples.