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] Added ability to define extra default password rules #40137

Merged
merged 3 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Illuminate/Validation/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class Password implements Rule, DataAwareRule, ValidatorAwareRule
*/
protected $compromisedThreshold = 0;

/**
* Additional validation rules that should be merged into the default rules during validation.
*
* @var array
*/
protected $customRules = [];

/**
* The failure messages, if any.
*
Expand Down Expand Up @@ -259,6 +266,19 @@ public function symbols()
return $this;
}

/**
* Specify additional validation rules that should be merged with the default rules during validation.
*
* @param string|array $rules
* @return $this
*/
public function rules($rules)
{
$this->customRules = Arr::wrap($rules);

return $this;
}

/**
* Determine if the validation rule passes.
*
Expand All @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Validation/ValidationPasswordRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,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, []);
Expand Down