Skip to content

Commit 8e758f7

Browse files
faisuctaylorotwell
andauthored
[12.x] Fix Password::required() to fail when value is missing (#58125)
* Fix Password::required() to fail when value is missing * Update Password.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 54d9c8b commit 8e758f7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Illuminate/Validation/Rules/Password.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Container\Container;
66
use Illuminate\Contracts\Validation\DataAwareRule;
7+
use Illuminate\Contracts\Validation\ImplicitRule;
78
use Illuminate\Contracts\Validation\Rule;
89
use Illuminate\Contracts\Validation\UncompromisedVerifier;
910
use Illuminate\Contracts\Validation\ValidatorAwareRule;
@@ -12,7 +13,7 @@
1213
use Illuminate\Support\Traits\Conditionable;
1314
use InvalidArgumentException;
1415

15-
class Password implements Rule, DataAwareRule, ValidatorAwareRule
16+
class Password implements Rule, DataAwareRule, ImplicitRule, ValidatorAwareRule
1617
{
1718
use Conditionable;
1819

@@ -331,6 +332,14 @@ public function passes($attribute, $value)
331332
{
332333
$this->messages = [];
333334

335+
if (! $this->required && ! $this->sometimes && ($this->data === null || ! Arr::has($this->data, $attribute))) {
336+
return true;
337+
}
338+
339+
if ($value === null && ! $this->required && $this->validator && $this->validator->hasRule($attribute, ['Nullable'])) {
340+
return true;
341+
}
342+
334343
$validator = Validator::make(
335344
$this->data,
336345
[$attribute => [

tests/Validation/ValidationPasswordRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ public function testSometimes()
443443
$this->passes([Password::sometimes()], ['Password123', 'password123']);
444444
}
445445

446+
public function testRequiredWithMissingValue()
447+
{
448+
$v = new Validator(
449+
resolve('translator'),
450+
[],
451+
['password' => [Password::required()]]
452+
);
453+
454+
$this->assertFalse($v->passes());
455+
$this->assertArrayHasKey('password', $v->messages()->toArray());
456+
$this->assertStringContainsString('required', $v->messages()->first('password'));
457+
458+
$v = \Illuminate\Support\Facades\Validator::make(
459+
[],
460+
[
461+
'password' => [\Illuminate\Validation\Rules\Password::required()],
462+
]
463+
);
464+
465+
$this->assertFalse($v->passes());
466+
}
467+
446468
protected function passes($rule, $values)
447469
{
448470
$this->assertValidationRules($rule, $values, true, []);

0 commit comments

Comments
 (0)