chamber-orchestra / form-bundle
Symfony 8 bundle for JSON-first API form handling with controller traits, data transformers, Doctrine validation, and RFC 9457 problem details error responses
Package info
github.com/chamber-orchestra/form-bundle
Type:symfony-bundle
pkg:composer/chamber-orchestra/form-bundle
Requires
- php: ^8.5
- chamber-orchestra/view-bundle: 8.0.*
- doctrine/orm: 3.6.*
- symfony/clock: 8.0.*
- symfony/config: 8.0.*
- symfony/dependency-injection: 8.0.*
- symfony/form: 8.0.*
- symfony/framework-bundle: 8.0.*
- symfony/runtime: 8.0.*
- symfony/translation: 8.0.*
- symfony/validator: 8.0.*
Requires (Dev)
- doctrine/doctrine-bundle: ^3.2
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-doctrine: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^13.0
- symfony/test-pack: ^1.2
Conflicts
This package is auto-updated.
Last update: 2026-02-28 11:30:49 UTC
README
ChamberOrchestra Form Bundle
A Symfony 8 bundle that simplifies JSON-first form handling for REST APIs. It provides controller traits for the submit/validate/response flow, specialized API form types with CSRF disabled, reusable data transformers, Doctrine-backed uniqueness validation, and structured error responses following RFC 9457 (Problem Details for HTTP APIs).
Features
- Controller traits (
FormTrait,ApiFormTrait) for submit/validate/response flow with null-safe request handling - JSON payload handling for mutation requests with automatic merging of uploaded files
- API form base types (
QueryForm,MutationForm) with CSRF disabled and empty block prefixes for clean JSON payloads - Custom form types:
BooleanType,TimestampType,HiddenEntityTypewith secure query builder parameterization - Data transformers for booleans, Unix timestamps, comma-separated arrays, and JSON strings
- RFC 9457 problem details via
ValidationFailedViewwith structured violations for consistent API error responses - Translatable error normalizer (
ProblemNormalizer) for localized exception messages in problem detail responses UniqueFieldvalidation constraint for Doctrine repositories with multi-field checks, closure-based exclusions, and custom normalizersTelExtensionto strip non-digit characters from phone number inputCollectionUtilsfor syncing Doctrine collections (add new / remove stale items)
Requirements
- PHP ^8.5
- Symfony 8.0 components (framework-bundle, form, validator, config, dependency-injection, runtime, translation, clock)
- Doctrine ORM 3.6
- chamber-orchestra/view-bundle 8.0
Installation
composer require chamber-orchestra/form-bundle:8.0.*
Enable the bundle in config/bundles.php:
return [ // ... ChamberOrchestra\FormBundle\ChamberOrchestraFormBundle::class => ['all' => true], ];
Usage
Controller Traits
Use FormTrait for standard HTML form submissions and ApiFormTrait for JSON API endpoints.
handleFormCall() accepts a form class or instance, handles the request, and returns a view or response. handleApiCall() does the same for API endpoints -- it automatically parses JSON payloads for MutationForm types and merges uploaded files.
onFormSubmitted() checks validity, returns a ValidationFailedView on failure, or invokes the callback on success:
use ChamberOrchestra\FormBundle\ApiFormTrait; use ChamberOrchestra\ViewBundle\View\ViewInterface; use Symfony\Component\HttpFoundation\Request; final class SearchCourseAction { use ApiFormTrait; public function __invoke(Request $request): ViewInterface { $form = $this->createForm(SearchCourseForm::class); $form->submit($request->query->all()); return $this->onFormSubmitted($form, function (SearchCourseData $dto) use ($request) { $entities = $this->er->searchCourses( $pagination = $this->getPagination(['per_page_limit' => $this->getPerPageLimit($request)]), $dto->query, $dto->brands, $dto->topics, $dto->products, $dto->durations ); return new PaginatedView($entities, $pagination, CourseView::class); }); } }
API Form Types
Extend QueryForm for GET requests or MutationForm for POST/PUT/PATCH requests. Both disable CSRF protection and use empty block prefixes for clean JSON input/output.
Data Transformers
| Transformer | Description |
|---|---|
TextToBoolTransformer |
Converts "true", "1", "yes" to boolean |
DateTimeToNumberTransformer |
Converts Unix timestamps to DateTimeInterface objects |
ArrayToStringTransformer |
Converts arrays to/from comma-separated strings |
JsonStringToArrayTransformer |
Parses JSON strings to arrays (handles empty strings) |
HiddenEntityType
Loads Doctrine entities by ID from a hidden form field. Supports a custom query_builder option with secure parameterized queries.
UniqueField Validator
Validates field uniqueness against Doctrine repositories. Supports multiple fields, closure-based exclusions, custom normalizers, and targeted error paths.
ProblemNormalizer
Extends Symfony's ProblemNormalizer to translate exception messages when the exception implements TranslatableExceptionInterface. This ensures localized error messages in RFC 9457 problem detail responses.
Response Views
| View | HTTP Status | Description |
|---|---|---|
SuccessView |
200 | Empty success response |
ValidationFailedView |
422 | Form validation errors with structured ViolationView items |
FailureView |
Configurable | Generic error response |
RedirectView |
301/302 | Redirect response for AJAX requests |
SuccessHtmlView |
200 | HTML fragment response for AJAX requests |
Testing
Install dependencies and run the full test suite:
composer install ./vendor/bin/phpunit
The integration test kernel (tests/Integrational/TestKernel.php) boots a minimal Symfony application with in-memory SQLite for Doctrine tests.
License
MIT