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] Add ability to define default Password Rule #37387

Merged
merged 7 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions src/Illuminate/Validation/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Validation\UncompromisedVerifier;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;

class Password implements Rule, DataAwareRule
{
Expand Down Expand Up @@ -74,6 +75,13 @@ class Password implements Rule, DataAwareRule
*/
protected $messages = [];

/**
* The callback to be used for the Password's default rules.
*
* @var string|array|callable|null
*/
public static $defaultCallback;

/**
* Create a new rule instance.
*
Expand All @@ -85,6 +93,31 @@ public function __construct($min)
$this->min = max((int) $min, 1);
}

/**
* Set the default callback to be used for determining the Password's default rules.
*
* @param string|array|callable $callback
* @return $this
*/
public static function defaultUsing($callback)
{
if (! is_array($callback) && ! is_callable($callback)) {
throw new InvalidArgumentException('$callback should either be callable or an array');
}

static::$defaultCallback = $callback;
}

/**
* Get Password's default rules.
*
* @return array
*/
public static function fromDefault()
crynobone marked this conversation as resolved.
Show resolved Hide resolved
{
return Arr::wrap(value(static::$defaultCallback));
}

/**
* Set the data under validation.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Validation/ValidationPasswordRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ public function testMessagesOrder()
);
}

public function testItCanSetDefaultUsing()
{
$password = Password::min(2)->numbers();

Password::defaultUsing(function () use ($password) {
return $password;
});

$this->assertSame([$password], Password::fromDefault());

Password::defaultUsing(['required', 'password']);
$this->assertSame(['required', 'password'], Password::fromDefault());
}

public function testItCannotSetDefaultUsingGivenString()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('$callback should either be callable or an array');

Password::defaultUsing('required|password');
}

protected function passes($rule, $values)
{
$this->testRule($rule, $values, true, []);
Expand Down Expand Up @@ -227,5 +249,7 @@ protected function tearDown(): void
Facade::clearResolvedInstances();

Facade::setFacadeApplication(null);

Password::$defaultCallback = null;
}
}