Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix replacing required :input with null on PHP 8.1 #36622

Merged
merged 1 commit into from
Mar 17, 2021

Conversation

dshoreman
Copy link
Contributor

Fixes an issue where missing fields in the data being validated will cause a deprecation warning when formatting the message, because str_replace() expects a string but gets null:

if (is_scalar($actualValue) || is_null($actualValue)) {
$message = str_replace(':input', $this->getDisplayableValue($attribute, $actualValue), $message);
}


This is already covered but requires PHP > 8.0 to trigger:

public function testValidateThrowsOnFail()
{
$this->expectException(ValidationException::class);
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']);
$v->validate();
}

public function testValidateRequired()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [], ['name' => 'Required']);
$this->assertFalse($v->passes());

Full Stacktrace

 [2021-03-06 15:20:07] testing.ERROR: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated {"userId":35,"exception":"[object] (ErrorException(code: 0): str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated at ./vendor/laravel/framework/src/Illuminate/Validation/Concerns/FormatsMessages.php:310)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 ./vendor/laravel/framework/src/Illuminate/Validation/Concerns/FormatsMessages.php(310): str_replace()
#2 ./vendor/laravel/framework/src/Illuminate/Validation/Concerns/FormatsMessages.php(219): Illuminate\\Validation\\Validator->replaceInputPlaceholder()
#3 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(814): Illuminate\\Validation\\Validator->makeReplacements()
#4 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(566): Illuminate\\Validation\\Validator->addFailure()
#5 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(388): Illuminate\\Validation\\Validator->validateAttribute()
#6 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(419): Illuminate\\Validation\\Validator->passes()
#7 ./vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php(25): Illuminate\\Validation\\Validator->fails()
#8 ./vendor/laravel/framework/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php(30): Illuminate\\Foundation\\Http\\FormRequest->validateResolved()
#9 ./vendor/laravel/framework/src/Illuminate/Container/Container.php(1219): Illuminate\\Foundation\\Providers\\FormRequestServiceProvider->Illuminate\\Foundation\\Providers\\{closure}()
#10 ./vendor/laravel/framework/src/Illuminate/Container/Container.php(1183): Illuminate\\Container\\Container->fireCallbackArray()
#11 ./vendor/laravel/framework/src/Illuminate/Container/Container.php(1168): Illuminate\\Container\\Container->fireAfterResolvingCallbacks()
#12 ./vendor/laravel/framework/src/Illuminate/Container/Container.php(732): Illuminate\\Container\\Container->fireResolvingCallbacks()
#13 ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\\Container\\Container->resolve()
#14 ./vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\\Foundation\\Application->resolve()
#15 ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\\Container\\Container->make()
#16 ./vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(79): Illuminate\\Foundation\\Application->make()
#17 ./vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(48): Illuminate\\Routing\\ControllerDispatcher->transformDependency()
#18 ./vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(28): Illuminate\\Routing\\ControllerDispatcher->resolveMethodDependencies()
#19 ./vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(41): Illuminate\\Routing\\ControllerDispatcher->resolveClassMethodDependencies()
#20 ./vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#21 ./vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): Illuminate\\Routing\\Route->runController()
#22 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\\Routing\\Route->run()
#23 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#24 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#25 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#26 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#27 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(103): Illuminate\\Routing\\Middleware\\ThrottleRequests->handleRequest()
#28 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(55): Illuminate\\Routing\\Middleware\\ThrottleRequests->handleRequestUsingNamedLimiter()
#29 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle()
#30 ./vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(44): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#31 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Auth\\Middleware\\Authenticate->handle()
#32 ./vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(33): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->Laravel\\Sanctum\\Http\\Middleware\\{closure}()
#34 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 ./vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(34): Illuminate\\Pipeline\\Pipeline->then()
#36 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->handle()
#37 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#38 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\\Pipeline\\Pipeline->then()
#39 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(670): Illuminate\\Routing\\Router->runRouteWithinStack()
#40 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRoute()
#41 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute()
#42 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch()
#43 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#44 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#45 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#46 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#47 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#48 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#49 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#50 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#52 ./vendor/fruitcake/laravel-cors/src/HandleCors.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle()
#54 ./vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#55 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle()
#56 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#57 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then()
#58 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#59 ./vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(508): Illuminate\\Foundation\\Http\\Kernel->handle()
#60 ./vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(474): Illuminate\\Foundation\\Testing\\TestCase->call()
#61 ./vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(333): Illuminate\\Foundation\\Testing\\TestCase->json()
#62 ./tests/Feature/Api/Projects/CreateProjectTest.php(119): Illuminate\\Foundation\\Testing\\TestCase->postJson()
#63 ./vendor/phpunit/phpunit/src/Framework/TestCase.php(1526): Tests\\Feature\\Api\\Projects\\CreateProjectTest->cannot_create_project_without_name()
#64 ./vendor/phpunit/phpunit/src/Framework/TestCase.php(1132): PHPUnit\\Framework\\TestCase->runTest()
#65 ./vendor/phpunit/phpunit/src/Framework/TestResult.php(722): PHPUnit\\Framework\\TestCase->runBare()
#66 ./vendor/phpunit/phpunit/src/Framework/TestCase.php(884): PHPUnit\\Framework\\TestResult->run()
#67 ./vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\\Framework\\TestCase->run()
#68 ./vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\\Framework\\TestSuite->run()
#69 ./vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\\Framework\\TestSuite->run()
#70 ./vendor/phpunit/phpunit/src/TextUI/TestRunner.php(667): PHPUnit\\Framework\\TestSuite->run()
#71 ./vendor/phpunit/phpunit/src/TextUI/Command.php(142): PHPUnit\\TextUI\\TestRunner->run()
#72 ./vendor/phpunit/phpunit/src/TextUI/Command.php(95): PHPUnit\\TextUI\\Command->run()
#73 ./vendor/phpunit/phpunit/phpunit(61): PHPUnit\\TextUI\\Command::main()
#74 {main}
"} 

Fixes an issue where data lacking one or more of the fields under
validation will cause tests to fail with an `ErrorException`.

That exception is a PHP deprecation warning triggered by the call to
`str_replace()` inside `replaceInputPlaceholder()`, which assumes the
value returned from `getDisplayableValue()` will always be a string.
@taylorotwell taylorotwell merged commit 9d80189 into laravel:8.x Mar 17, 2021
@GrahamCampbell GrahamCampbell changed the title Fix replacing required :input with null on PHP 8.1 [8.x] Fix replacing required :input with null on PHP 8.1 Mar 17, 2021
@dshoreman dshoreman deleted the php81 branch March 21, 2021 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants