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

Conversation

crynobone
Copy link
Member

@crynobone crynobone commented May 17, 2021

This will make it easier to define a generic default password rules for login, register and password reset and useful for Laravel UI, Breeze, Jetstream and Nova.

Defining Rules

Typically on the App\Providers\AuthServiceProvider

use Illuminate\Validation\Rules\Password;

Password::defaultUsing(function() {
     return Password::min(8)->uncompromised();
});

Using the rules

E.g in Laravel Nova.

use Laravel\Nova\Fields\Password;
use Illuminate\Validation\Rules\Password as PasswordRule;

Password::make('Password', 'password')->rules(PasswordRule::default());

Also, you can use:

Password::required(); // return ['required', Password::min(8)->uncompromised()];

Password::sometimes(); // return ['sometimes', Password::min(8)->uncompromised()];

Signed-off-by: Mior Muhammad Zaki crynobone@gmail.com

This will make it easier to define a generic default password rules for login, register and password reset and useful for Laravel UI, Breeze, Jetstream and Nova.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
crynobone and others added 2 commits May 17, 2021 19:26
Co-authored-by: Dries Vints <dries.vints@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@driesvints
Copy link
Member

driesvints commented May 17, 2021

Would be cool if you could do:

Password::default()->required();

Or even:

Password::required()

Which will apply the default by default 😅

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@crynobone
Copy link
Member Author

Password::required()

I really liking this, Password::required() for login and reset password, Password::sometimes() for change password.

```
 1) Warning
Test method "testRule" in test class "Illuminate\Tests\Validation\ValidationPasswordRuleTest" is not public.
```

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@GrahamCampbell GrahamCampbell changed the title [8.x] Add ability to define default Password Rule. [8.x] Add ability to define default Password Rule May 17, 2021
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@crynobone crynobone marked this pull request as ready for review May 18, 2021 06:41
@taylorotwell
Copy link
Member

So, if you use Password::required, I guess you are supposed to do something like this when building your validation rules?

'name' => ['required', 'string'],
'password' => [...Password::required(), 'foo', 'bar'],

@crynobone
Copy link
Member Author

crynobone commented May 18, 2021

@taylorotwell yes, that's the plan with returning it as array.

Was tempting to do the following, but not sure if it's wise

public static function __callStatic($method, $parameters)
{
    return [$method, static::default()];
}

@taylorotwell taylorotwell merged commit b42be88 into laravel:8.x May 18, 2021
@crynobone crynobone deleted the password-defaults branch May 18, 2021 15:45
@lloricode
Copy link
Contributor

Hi, did you just forget confirmed rule?

@crynobone
Copy link
Member Author

Hi, did you just forget confirmed rule?

Intentionally leave it out, you can do [...Password::required(), 'confirmed'] for specific use case.

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.

None yet

4 participants