Fluent Form Builder
Build forms programmatically using Polyform's fluent API for complete control over form structure and configuration.
Overview
The FormBuilder class provides a fluent, expressive API for creating forms programmatically. This is ideal for seeding databases, creating forms in migrations or building forms dynamically based on application logic.
Getting Started
Start building a form using the Polyform facade or by instantiating FormBuilder directly. Every form must be associated with a "formable" model.
Form Configuration
Configure form-level settings using chainable methods. Only name() is required; all other settings are optional.
Configuration Methods
Multi-Step Forms
Create multi-step forms using addStep(). Fields added after a step are automatically assigned to that step until the next addStep() call.
Adding Fields
Polyform provides 26+ convenience methods for adding fields. Each method returns a field configurator for chainable field-level settings.
All Field Methods
Text Fields
Select Fields
addSelectFromEnum(), addSelectFromModel() or addSelectFromResolver() for dynamic option sources. See the Option Sources documentation for details. Choice Fields
File Upload Fields
Rating & Scale Fields
Date Fields
Other Fields
Generic addField() Method
For complete control or custom field types, use the generic addField() method. This accepts any field option, including conditional logic and custom validation rules.
Creating from Templates
Duplicate an existing form's structure using fromTemplate(). This copies all fields, steps and settings while allowing you to override specific properties.
Comprehensive Example
A complete multi-step conference registration form demonstrating most features:
Building the Form
Call build() to persist the form and its fields to the database. This method returns the created Form model instance.
build() method creates multiple database records (Form, FormSteps, FormFields). Consider wrapping it in a database transaction if you need atomic form creation.