The Laravel Dry Requests package checks if your requests would pass validation if you executed them normally. Think of it as the equivalent of a --dry-run
CLI flag for request validation. Using this package, you can hit the endpoint users are using to enter a form and get real-time feedback with 100% validation accuracy:
The way it works under the hood is that it runs validation logic for a controller but skips the controller action. This package returns a 200 OK
status response when given an X-Dry-Run: true
header. Fields not present get omitted dynamically using the sometimes
rule to ensure good UX.
You can start using this validation package by either adding a DryRunnable
trait to a form request or using $request->validate()
directly:
class StoreUserRequest extends FormRequest{ use DryRunnable; public function rules(): array { return [ 'email' => ['required', 'email', 'max:255', 'unique:users'], 'username' => ['required', 'string', 'min:2', 'max:255', 'unique:users'], 'nickname' => ['nullable', 'string', 'min:2', 'max:255'], ]; }}
On the frontend, you need to send the X-Dry-Run
header to validate form input before submitting the whole form:
// 1. "Username is unavailable" validation erroraxios.post( '/users', { username: 'Agent007' }, { headers: { 'X-Dry-Run': true } } ) .then(response => response.status);
To learn more about all the features in this package, including Inertia.js support and advanced features, check out the laravel-dry-requests package on GitHub.
0 comments:
Post a Comment
Thanks