diff --git a/README.md b/README.md index 5177f18..0c12ad9 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ Password::make('password') As with the copy to clipboard action button, you can have this action rendered inline on the input as well by calling the `inlineSuffix()` method on the input. -### Password Generation +### Password Generation Method By default, the password generation is handled with Laravel's `Str::password()` helper method. This will generate a random, strong password that is 32 characters long for you. If you have a `maxLength()` set on the input, that length will be used instead for the character length. @@ -208,6 +208,20 @@ Password::make('password') }), ``` +### Password Max Length + +When using the default password generator (`Str::password()`), we will tell it to use the `maxLength()` that is set on the input. This means that +if you set a maximum length of 10 characters, the password generated by this action will be 10 characters long. By default, it is 32 characters long +if a max length is not set. + +```php +Password::make('password') + ->regeneratePassword() + ->maxLength(10), +``` + +> **Note:** Due to how Laravel's `Str::password()` helper works, the password max length must be a minimum of 3 characters long. + Now when the button is clicked, `my-custom-password` will be filled into the input instead. ### Button Icon diff --git a/src/Concerns/CanRegeneratePassword.php b/src/Concerns/CanRegeneratePassword.php index ba3a14d..755ed05 100644 --- a/src/Concerns/CanRegeneratePassword.php +++ b/src/Concerns/CanRegeneratePassword.php @@ -111,7 +111,7 @@ public function generateNewSecret(mixed $state): string $maxLength = $this->getMaxLength() ?? 32; - return Str::password($maxLength); + return Str::password(max(3, $maxLength)); } public function getRegeneratePasswordAction(): Action diff --git a/tests/Concerns/CanRegeneratePasswordTest.php b/tests/Concerns/CanRegeneratePasswordTest.php index 803280b..1bb45a9 100644 --- a/tests/Concerns/CanRegeneratePasswordTest.php +++ b/tests/Concerns/CanRegeneratePasswordTest.php @@ -63,11 +63,22 @@ it("respects the input's maxlength when generating a new password with the default generator", function () { $input = Password::make('password') - ->maxlength(2); + ->maxlength(3); - expect($input->generateNewSecret(null))->toHaveLength(2); + expect($input->generateNewSecret(null))->toHaveLength(3); }); +test('a minimum of 3 characters is required when using the default password generator helper on the string class', function (int $minLength) { + $input = Password::make('password') + ->maxLength($minLength); + + expect($input->generateNewSecret(null))->toHaveLength(3); +})->with([ + 0, + 1, + 2, +]); + class CanRegenerateWithButton extends Livewire { public function form(Form $form): Form