Conditional Validation Rule Support Added in Laravel 8.55

News

August 18th, 2021

laravel8.jpg

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:

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:

1request()->validate([
2 'name' => 'required|string|min:6',
3 'password' => [
4 'required',
5 'string',
6 Rule::when(true, ['min:5', 'confirmed'])
7 ],
8]);

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:

1Route::post('/user/{user}', function (ImplicitBindingModel $user) {
2 return $user;
3})->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:

1$validator->safe()->only(['name', 'email']);
2$validator->safe()->except([...]);
3 
4$formRequest->safe()->only([...]);
5$formRequest->safe()->except([...]);

Stringable Support for isUuid()

Andrew Minion contributed stringable support for Str::isUuid():

1// Before
2Str::isUuid(Str::of($filename)->after('-')->before('.'))
3 
4// After
5Str::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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.