- Can I use this bundle with Laravel, or is it strictly for Symfony?
- This bundle is designed for Symfony, not Laravel. Laravel developers should explore alternatives like `laravel-form-request-validation` or `spatie/laravel-form-requests` for JSON form handling. The bundle leverages Symfony’s Form component, which isn’t natively available in Laravel’s ecosystem.
- How do I install this bundle in a Symfony project?
- Run `composer require elao/json-http-form-bundle` in your Symfony project’s root directory. The bundle will automatically register itself if you’ve followed Symfony’s bundle installation best practices (e.g., placing it in the `bundles.php` configuration). No additional steps are required for basic functionality.
- Will this bundle work with Laravel’s API resources or FormRequest classes?
- No, this bundle is incompatible with Laravel’s FormRequest or API resources. It’s built for Symfony’s Form component and won’t integrate with Laravel’s request handling pipeline. For Laravel, consider using `laravel-form-request-validation` or custom middleware to parse JSON payloads.
- Does this bundle support nested JSON structures or complex form types?
- Yes, the bundle supports nested JSON structures and complex form types like collections, nested forms, and dynamic fields. However, ensure your JSON payload matches the form’s expected structure. For example, a nested `RocketType` with `colors` as an array will work if the JSON includes a `colors` array with valid values.
- How does this bundle handle validation errors for JSON requests?
- Validation errors for JSON requests are handled the same way as standard Symfony forms. If the form fails validation, you can access errors via `$form->getErrors()` or serialize them to JSON using Symfony’s `JsonResponse`. For APIs, you may need to customize error serialization (e.g., using `JsonHttpExceptionListener` or a custom error handler).
- Is there a performance impact when using JSON form submissions?
- The performance impact is minimal, typically adding 1–5ms per request for JSON decoding. However, complex forms with deep nesting or large payloads may introduce slight overhead. Benchmark your specific use case, especially for high-throughput APIs, and consider caching form definitions if needed.
- Can I mix JSON and traditional form submissions for the same endpoint?
- Yes, the bundle automatically detects the `Content-Type` header. If the request is JSON (e.g., `application/json`), it decodes the body; otherwise, it falls back to Symfony’s default `HttpFoundation` handling. This allows the same endpoint to accept both HTML form submissions and JSON payloads without modification.
- How do I test JSON form submissions in PHPUnit?
- Use Symfony’s `Client` class to simulate JSON requests. For example, `$client->request('POST', '/api/rocket', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode(['name' => 'Test', 'colors' => ['white']]));`. Ensure your tests cover both JSON and traditional form submissions to verify consistency.
- Does this bundle support PUT/PATCH/DELETE requests with JSON payloads?
- Yes, the bundle supports JSON payloads for `PUT`, `PATCH`, and `DELETE` requests, just like `POST`. The `JsonHttpRequestListener` automatically decodes the request body if the `Content-Type` is `application/json`, making it ideal for RESTful APIs where these methods are used for updates or deletions.
- Are there any security considerations when using JSON form submissions?
- Yes, ensure JSON payloads are validated using Symfony’s `Validator` component to prevent malicious data. If using CSRF protection, include the token in the JSON payload (e.g., `_csrf_token`). Additionally, sanitize or validate untrusted JSON inputs, as the bundle relies on Symfony’s default validation, which may not cover all edge cases.