From 146c2ad3b87f7d53d348316fa5e1f875bb996247 Mon Sep 17 00:00:00 2001 From: Ash Allen Date: Wed, 22 Dec 2021 15:50:10 +0000 Subject: [PATCH 1/3] Added ability to define custom default password rules. --- src/Illuminate/Validation/Rules/Password.php | 22 +++++++++++- .../Validation/ValidationPasswordRuleTest.php | 36 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Rules/Password.php b/src/Illuminate/Validation/Rules/Password.php index cce6097f3a5e..0b9b69bb8f61 100644 --- a/src/Illuminate/Validation/Rules/Password.php +++ b/src/Illuminate/Validation/Rules/Password.php @@ -79,6 +79,13 @@ class Password implements Rule, DataAwareRule, ValidatorAwareRule */ protected $compromisedThreshold = 0; + /** + * Custom validation rules that should be used. + * + * @var array + */ + protected $customRules = []; + /** * The failure messages, if any. * @@ -259,6 +266,19 @@ public function symbols() return $this; } + /** + * Define custom validation rules that should be used. + * + * @param $rules + * @return Password + */ + public function rules($rules) + { + $this->customRules = Arr::wrap($rules); + + return $this; + } + /** * Determine if the validation rule passes. * @@ -272,7 +292,7 @@ public function passes($attribute, $value) $validator = Validator::make( $this->data, - [$attribute => 'string|min:'.$this->min], + [$attribute => array_merge(['string', 'min:'.$this->min], $this->customRules)], $this->validator->customMessages, $this->validator->customAttributes )->after(function ($validator) use ($attribute, $value) { diff --git a/tests/Validation/ValidationPasswordRuleTest.php b/tests/Validation/ValidationPasswordRuleTest.php index d81ea553b722..bef21b132999 100644 --- a/tests/Validation/ValidationPasswordRuleTest.php +++ b/tests/Validation/ValidationPasswordRuleTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Facade; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; +use Illuminate\Validation\Rule; use Illuminate\Validation\Rules\Password; use Illuminate\Validation\ValidationServiceProvider; use Illuminate\Validation\Validator; @@ -269,6 +270,41 @@ public function testItPassesWithValidDataIfTheSameValidationRulesAreReused() $this->assertTrue($v1->passes()); } + public function testPassesWithCustomRules() + { + $closureRule = function ($attribute, $value, $fail) { + if ($value !== 'aa') { + $fail('Custom rule closure failed'); + } + }; + + $ruleObject = new class implements \Illuminate\Contracts\Validation\Rule + { + public function passes($attribute, $value) + { + return $value === 'aa'; + } + + public function message() + { + return 'Custom rule object failed'; + } + }; + + $this->passes(Password::min(2)->rules($closureRule), ['aa']); + $this->passes(Password::min(2)->rules([$closureRule]), ['aa']); + $this->passes(Password::min(2)->rules($ruleObject), ['aa']); + $this->passes(Password::min(2)->rules([$closureRule, $ruleObject]), ['aa']); + + $this->fails(Password::min(2)->rules($closureRule), ['ab'], [ + 'Custom rule closure failed' + ]); + + $this->fails(Password::min(2)->rules($ruleObject), ['ab'], [ + 'Custom rule object failed' + ]); + } + protected function passes($rule, $values) { $this->assertValidationRules($rule, $values, true, []); From 762d948303f30f4d360f46f09830b736b93eb629 Mon Sep 17 00:00:00 2001 From: Ash Allen Date: Wed, 22 Dec 2021 17:17:29 +0000 Subject: [PATCH 2/3] Style updates. --- tests/Validation/ValidationPasswordRuleTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Validation/ValidationPasswordRuleTest.php b/tests/Validation/ValidationPasswordRuleTest.php index bef21b132999..35acd4a25fd0 100644 --- a/tests/Validation/ValidationPasswordRuleTest.php +++ b/tests/Validation/ValidationPasswordRuleTest.php @@ -6,7 +6,6 @@ use Illuminate\Support\Facades\Facade; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; -use Illuminate\Validation\Rule; use Illuminate\Validation\Rules\Password; use Illuminate\Validation\ValidationServiceProvider; use Illuminate\Validation\Validator; @@ -297,11 +296,11 @@ public function message() $this->passes(Password::min(2)->rules([$closureRule, $ruleObject]), ['aa']); $this->fails(Password::min(2)->rules($closureRule), ['ab'], [ - 'Custom rule closure failed' + 'Custom rule closure failed', ]); $this->fails(Password::min(2)->rules($ruleObject), ['ab'], [ - 'Custom rule object failed' + 'Custom rule object failed', ]); } From 5b960c779ef12ccba7f7b47d5c8b31c7f5c28dd9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Dec 2021 13:24:24 -0600 Subject: [PATCH 3/3] formatting --- src/Illuminate/Validation/Rules/Password.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Rules/Password.php b/src/Illuminate/Validation/Rules/Password.php index 0b9b69bb8f61..676dda6f49f3 100644 --- a/src/Illuminate/Validation/Rules/Password.php +++ b/src/Illuminate/Validation/Rules/Password.php @@ -80,7 +80,7 @@ class Password implements Rule, DataAwareRule, ValidatorAwareRule protected $compromisedThreshold = 0; /** - * Custom validation rules that should be used. + * Additional validation rules that should be merged into the default rules during validation. * * @var array */ @@ -267,10 +267,10 @@ public function symbols() } /** - * Define custom validation rules that should be used. + * Specify additional validation rules that should be merged with the default rules during validation. * - * @param $rules - * @return Password + * @param string|array $rules + * @return $this */ public function rules($rules) {