Skip to content

Showing forms on the website

A published form can be placed on a page in two ways. Both render the same published version with the same theme.

Every published form gets a page right away, at /forms/{form name} (for example /forms/contact). No document, template or console command is needed. Drafts and archived forms answer 404. To move or turn off these pages:

pimkit_forms:
form_pages:
enabled: true # false: no pages, only areabrick and Twig
prefix: '/forms' # for example '/kontakt-formulare'

The page uses @PimKitForms/page/form.html.twig. Override it in templates/bundles/PimKitFormsBundle/page/form.html.twig to put the form in your site layout.

Editors add the PimKit form brick (pimkit-form) to any areablock, open its settings and pick a published form. The settings also have an optional success message that replaces the form’s own on that page. In edit mode the brick shows a placeholder with the form name instead of the form.

{{ pimkit_form('contact') }}
{{ pimkit_form('contact', {theme: 'plain', locale: app.request.locale}) }}

Options:

Option Meaning
theme Theme folder name. Default: pimkit_forms.default_theme (plain)
locale Language of labels and texts. Default: the request locale
successMessage Replaces the form’s success message
object Data object for fields that fill in from “Data object of the page” (prefill)
prefill Starting values by field name, e.g. { topic: 'support' }. Win over the field’s own source
step Opens this step of a multi-step form (0 is the first)
attr Extra data-*, aria-* or class attributes on the <form> tag. Anything else is dropped
values, errors, formErrors Used when a form is shown again after a failed no-JS submit

A form that does not exist, is not published or is archived renders nothing, so a page never breaks. A warning goes to the log, and in debug mode an HTML comment says why.

In PHP, use PimKit\FormsBundle\Render\FormRenderer::render($name, $options).

  • Every visible field has a <label>. Required fields say so in text, not only by colour.
  • Help and error texts are linked to the input with aria-describedby. Inputs with errors get aria-invalid="true".
  • After a failed submit, an error summary with role="alert" lists the problems and links to each field.
  • HTML5 attributes (required, maxlength, minlength, pattern, min, max, step) come from the field settings. The server checks everything again on submit.
  • Fields hidden by conditions are rendered with the hidden attribute and their rules in data-pimkit-conditions, for the frontend script.
  • Hidden fields get their value from the current request (query, header or cookie) as set in the editor.
  • Consent text keeps only safe tags (a, strong, em, b, i, u, br, span, p). Scripts and event attributes are removed.
  • Anti-spam inputs: a honeypot (_pk_website) and a signed form token (_pk_token, see Submitting forms). No session is started, so pages with forms stay cacheable.
  • The same form twice on one page gets different element ids (pk-contact, pk-contact-2).
  • Field types without a template yet are skipped.
  • The first form on a page adds <script src="/bundles/pimkitforms/frontend/pimkit-forms.min.js" defer> (live conditions, checks in the browser, AJAX submit, file drop zone). See Conditional logic and the frontend script.

The plain theme has no CSS of its own, so the first plain form on a page adds the PimKit stylesheet for a clean default look:

<link rel="stylesheet" href="/bundles/pimkitforms/frontend/pimkit-forms.css">

Turn it off with pimkit_forms.frontend_styles: false (or {{ pimkit_form('contact', {styles: false}) }} for one form) when your site styles the forms. It only styles .pk-form classes. Change colours by overriding the custom properties (--pk-focus, --pk-error, --pk-border, …) on .pk-form.

Themes live in templates/themes/<theme>/:

  • form.html.twig: the form tag, rows, anti-spam inputs and submit button
  • errors.html.twig: the error summary
  • field_<type>.html.twig: one per field type. The plain ones extend field_base.html.twig

A theme only needs the templates it changes. Anything missing falls back to plain.

Built-in themes: plain, bootstrap5, tailwind

Section titled “Built-in themes: plain, bootstrap5, tailwind”

A published form with the bootstrap5 theme

bootstrap5 and tailwind use the plain templates and add their own classes from src/Render/ThemeClasses.php (form-control, is-invalid, btn btn-primary, … or Tailwind utilities). The pk-form__* classes stay, because the frontend script needs them. The form tag gets data-pimkit-classes with the classes the script adds itself (errors, error summary, invalid inputs, success message).

{{ pimkit_form('contact', {theme: 'bootstrap5', locale: app.request.locale}) }}

Load the framework’s CSS on the page as usual. Tailwind builds only keep classes they find: add vendor/pimkit/forms/src/Render/ThemeClasses.php to the content list of tailwind.config.js. Set the default for all forms with pimkit_forms.default_theme.

To change a built-in template in a project, copy it to templates/bundles/PimKitFormsBundle/themes/plain/... (the standard Symfony bundle override path). To add a theme, put it in templates/bundles/PimKitFormsBundle/themes/<name>/ and pass theme: '<name>'.

tests/Unit/Render/FormRendererTest.php compares the HTML with snapshots in tests/fixtures/render/. After a deliberate template change, run the tests with UPDATE_SNAPSHOTS=1 and review the diff.