Conditional Validation Rule Support Added in Laravel 8.55
Published on by Paul Redmond
The Laravel team released 8.55 with conditional validation rule support, trashed routes to include soft-deleted records and the latest changes in the 8.x branch.
This release has various features that are the result of community feedback directly with Taylor Otwell, who asked for "small things that could provide a nice developer experience" on Twitter:
Give me a Laravel papercut.
— Taylor Otwell 🥭 (@taylorotwell) August 11, 2021
A small thing that could be fixed to provide a nice developer experience improvement.
If I see a good one I will try to fix it *this afternoon*. ✂️
The Tweet replies are worth looking to see all the ideas and features the community is thinking about. While not all opinions are suitable for the framework, perhaps some features are ideal as Laravel packages.
Conditional Validation Rules
Taylor Otwell contributed conditional validation rule support (hat-tip to Tim MacDonald) that will only validate rules when a condition evaluates to true
:
request()->validate([ 'name' => 'required|string|min:6', 'password' => [ 'required', 'string', Rule::when(true, ['min:5', 'confirmed']) ],]);
In the above, the min:5
and confirmed
validation rules only apply when the first argument of Rule::when()
is true
. Check out Pull Request #38361 for implementation details.
Support "With Trashed" On Routes
Taylor Otwell contributed the route withTrashed()
method that allows soft-deleted models when resolving implicit model bindings:
Route::post('/user/{user}', function (ImplicitBindingModel $user) { return $user;})->middleware(['web'])->withTrashed();
The withTrashed()
method is useful to remove some of the boilerplate around getting bound model data to include deleted records. Check out Pull Request #38348 for more details.
Validated Subsets
Taylor Otwell contributed convenience methods for getting subsets of valid data from a validator:
$validator->safe()->only(['name', 'email']);$validator->safe()->except([...]); $formRequest->safe()->only([...]);$formRequest->safe()->except([...]);
Stringable Support for isUuid()
Andrew Minion contributed stringable support for Str::isUuid()
:
// BeforeStr::isUuid(Str::of($filename)->after('-')->before('.')) // AfterStr::of($filename)->after('-')->before('.')->isUuid()
Release Notes
You can see the full list of new features and updates below and the diff between 8.54.0 and 8.55.0 on GitHub.