elao/json-http-form-bundle
Symfony bundle that lets Forms handle JSON POST/PUT/PATCH/DELETE requests. Automatically detects JSON content-type, decodes request body, and submits data to your form; falls back to default HttpFoundation handling for normal GET/POST.
elao/json-http-form-bundle extends Symfony Forms to support JSON payloads (e.g., POST /api/submit with Content-Type: application/json), making it ideal for:
Form component, Validator, and Serializer, reducing friction for teams already using these tools. Aligns with Symfony’s declarative configuration patterns.JsonHttpRequestListener and modifies FormFactory to parse JSON input. No invasive changes to existing controllers or services.symfony/form, symfony/validator, symfony/serializer.Form extensions (e.g., other request parsers).JsonHttpRequestListener middleware).Constraint validation or custom validators to enforce schemas.CsrfTokenManager). Ensure tokens are included in JSON payloads if enabled.application/x-www-form-urlencoded and JSON for the same resource.)ValidationException) be serialized for JSON responses? (Bundle provides JsonHttpExceptionListener but may need customization.)ApiPlatform or NelmioApiBundle better serve the use case with built-in JSON support?Form and Validator components. Poor fit for:
FormRequest).nelmio/api-doc for automatic JSON schema generation.Symfony Panther or PHPUnit for JSON form testing.POST /api/orders).// src/Controller/OrderController.php
use Elao\JsonHttpFormBundle\Form\JsonHttpRequestListener;
#[Route('/api/orders', methods: ['POST'])]
public function submitOrder(Request $request, FormFactoryInterface $formFactory): Response
{
$form = $formFactory->create(OrderType::class);
$form->submit(json_decode($request->getContent(), true));
if ($form->isSubmitted() && $form->isValid()) {
// Process order...
}
return new JsonResponse($form->getErrors());
}
FormHandler or Controller logic to use JsonHttpRequestListener.x-www-form-urlencoded endpoints.TextType, CollectionType, etc.), but dynamic forms (e.g., DynamicFormType) may need custom handling.Request parsing (e.g., api-platform/core).composer require elao/json-http-form-bundle).JsonHttpRequestListener in config/packages/elao_json_http_form.yaml:
elao_json_http_form:
enabled: true
# Optional: Custom error formats
error_format: 'json'
JsonHttpRequestListener integration tests).JsonHttpRequestListener for custom JSON parsing (e.g., nested arrays).JsonHttpExceptionListener for tailored error responses.Form component evolves (e.g., PHP 8.1+ features).Profiler to inspect form submission lifecycle.JsonHttpRequestListener events for troubleshooting malformed JSON.Validator caching.k6 to validate throughput.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malformed JSON payload | 500 Internal Server Error |
Use try-catch around json_decode(); return 400 Bad Request. |
| Missing required form fields | Validation errors | Customize JsonHttpExceptionListener for user-friendly messages. |
Symfony Validator misconfiguration |
Silent failures or incorrect errors | Test with edge cases (e.g., empty arrays). |
| CSRF token mismatch (if enabled) | 403 Forbidden |
Ensure tokens are included in JSON payloads. |
| Bundle-Symfony version mismatch | Runtime exceptions | Pin Symfony version in composer.json. |
Form component basics.JsonSchema constraint).JsonHttpRequestListener events.Form docs + bundle’s source code.JsonHttpRequestListener coverage).How can I help you explore Laravel packages today?