Laravel API Validation: Form Requests and 422 Error Responses
To validate Laravel API requests consistently, put rules in a Form Request and pass only validated data to persistence. When a request expects JSON, Laravel returns a 422 status with field errors by default. Browser forms and APIs can receive different failure responses, so define what each client expects.
To validate Laravel API requests consistently, put rules in a Form Request and pass only validated data to persistence. When a request expects JSON, Laravel returns a 422 status with field errors by default. Browser forms and APIs can receive different failure responses, so define what each client expects.
This guide follows Laravel 13.x documentation and covers Form Request rules and authorization, validated input, 422 responses, and tests. Exact behavior depends on whether the request expects JSON and on the application's exception configuration.
Put validation in a Form Request
For a small check, a controller can use the request's validate method. When rules grow or request-specific authorization is needed, a Form Request keeps rules and authorization in a dedicated request class. Generate it with php artisan make:request; Laravel normally places it in app/Http/Requests.
Declare field constraints in rules() and check whether the authenticated user may perform the action in authorize(). Being authenticated does not automatically grant permission to update a particular record; use the applicable policy or ownership rule.
public function authorize(): bool
{
return $this->user()?->can('update', $this->route('post')) ?? false;
}
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'body' => ['required', 'string'],
];
}This is illustrative. Adapt policy names, route model binding, and nullable rules to the application.
Persist only validated input
When a Form Request is type-hinted by a controller method, Laravel validates it before calling the controller. On success, retrieve data with validated() or safe(). Passing the entire request to model creation or update may let fields outside the validation rules enter the persistence path.
public function store(StorePostRequest $request)
{
$validated = $request->validated();
$post = Post::create($validated);
return response()->json($post, 201);
}Also check the model's fillable/guarded settings and domain rules. Values clients must not control, such as author IDs or admin flags, should be set server-side. Validation checks input constraints; it does not replace authentication, authorization, or all business rules.
Define the API error contract
Failure behavior depends on whether the request expects JSON. For JSON requests, Laravel returns 422 Unprocessable Entity with an error object; nested error keys may use dot notation. Traditional browser form submissions can redirect back and flash errors to the session.
Set request headers and conditions to match the API contract, then verify the response. Localize user-facing messages as needed, but do not expose internal exceptions or sensitive data. Test custom exception formatting if used.
Verify with HTTP tests
Feature tests should send requests through the real route and check success and failure. For valid input, assert status, response body, and persisted data. For missing or malformed input, assert 422 and the errors field. Keep test configuration and database separate from production data.
$response = $this->postJson('/api/posts', [
'title' => '',
'body' => 'Example',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors(['title']);Match route, authentication, and Accept header to real API use. Test authorization failures separately so a 403 or 404 is not confused with validation failure.
Troubleshoot in order: use php artisan route:list to check URI, method, middleware, and handler; inspect JSON expectations and Accept when response type differs; pass only validated()/safe() data and review model mass-assignment rules; compare validation rule keys with nested JSON; then check Form Request authorize and policies.
A Form Request gathers validation and request-specific authorization, while validated input keeps persistence bounded. Test the success, 422, and authorization contracts together through HTTP tests.
References: official Laravel 13.x documentation on routing, validation, controllers, the service container, and testing.
References
[Laravel 13.x: Routing](https://laravel.com/docs/13.x/routing)
[Laravel 13.x: Validation](https://laravel.com/docs/13.x/validation)
[Laravel 13.x: Controllers](https://laravel.com/docs/13.x/controllers)
[Laravel 13.x: Service Container](https://laravel.com/docs/13.x/container)
[Laravel 13.x: Testing](https://laravel.com/docs/13.x/testing)