Mastering Contao’s Form Generator: Conditional Fields and Server‑Side Validation
Contao’s Form Generator lets you create conditional fields and enforce server‑side validation without custom code. Follow this guide to set up a dropdown‑triggered field, verify frontend and backend behavior, and understand the trade‑offs.
13 Jan 2026, 02:21 UTC

Why Conditional Forms Matter in Contao
When a website needs to adapt to user input—showing extra fields only when a certain option is chosen—developers often turn to custom JavaScript or third‑party extensions. Contao’s core Form Generator already handles this scenario. It lets you add fields, set visibility rules, and rely on Symfony’s validator for robust server‑side checks, all without writing a line of PHP.
Thesis: Build Dynamic Forms the Easy Way
By using the Form Generator’s drag‑and‑drop interface and built‑in conditional logic, you can:
- Show/hide fields on the fly with minimal JavaScript.
- Guarantee data integrity even if JavaScript is disabled.
- Persist submissions in
tl_form_datafor later export or CRM integration.
Step 1 – Create a Conditional Field Set
1. Log into the Contao backend and navigate to Forms.
2. Click Add Form and give it a name, e.g., Contact Form.
3. Add a Dropdown field called Country with options: "Germany", "France", "Other".
4. Add a Text field called Other Country and set it as Required.
5. In the Other Country field’s settings, click Conditional visibility:
- Trigger field:
Country - Trigger value:
Other - Action:
Show
Behind the scenes, Contao stores the form definition as a serialized array in tl_form and the field rules in tl_form_field. The conditional rule is saved as a JSON‑like string that the JavaScript engine reads when the form renders.
Step 2 – Verify Frontend Behavior
Open the form on the frontend. Select Germany; the Other Country field stays hidden. Switch to Other and the field appears instantly. This is powered by Contao’s FormGeneratorBundle JavaScript, which listens to change events and toggles the data-form-field attributes.
Step 3 – Test Server‑Side Validation
Even if a visitor disables JavaScript or submits via a tool like curl, the server still validates the data. The key is Symfony’s Validator component, integrated into Contao 4+.
# Example curl request – missing required field
curl -X POST \
-F 'form_id=1' \
-F 'country=Other' \
http://example.com/contao/form.php
Expected response: a JSON object with an error for Other Country stating it is required. This confirms that the rule is enforced server‑side.
Step 4 – Check Database Persistence
After a successful submission, log into MySQL and query:
SELECT * FROM tl_form_data WHERE pid = 1 ORDER BY id DESC LIMIT 1;
The result should contain a row with a serialized data column holding the user’s input. You can unserialize it in PHP to inspect the values.
Trade‑Offs and Limitations
- Complex Rules Are Hard to Debug: Conditional logic is stored as a JSON‑like string. If a field behaves unexpectedly, you may need to inspect the raw
tl_form_fieldentry to see the rule. - File Uploads Require Server Tuning: The
Filefield respects PHP’supload_max_filesizeandpost_max_size. Large files can be silently rejected if these limits aren’t increased. - No Custom Server‑Side Logic: If you need to run custom PHP code on submission, the Form Generator alone won’t suffice. You’d need a custom module or hook.
Actionable Takeaway
Use the Form Generator for most dynamic forms: it handles visibility, validation, and persistence out of the box. For advanced scenarios, consider a hybrid approach—use the generator for the UI and a custom hook for special processing. Always test both JavaScript‑enabled and disabled paths to ensure data integrity.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.