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

[12.x] Introducing Rules\Password::appliedRules Method #55206

Conversation

devajmeireles
Copy link
Contributor

@devajmeireles devajmeireles commented Mar 30, 2025

We know that the most interesting way to standardize password validation is through Rules/Password for backend.

However, when it comes to, for example, displaying the same validation rules from the backend on the frontend, we have a block due to the lack of a method that makes it possible to obtain the rules applied via AppServiceProvider.

This PR introduces the Rules\Password::appliedRules method that makes it possible to have an array containing the validation rules and their respective usage statuses, which allow us to access what was defined in the backend to display the rules that are actually accepted in a given password field.

Example:

class AppServiceProvider extends ServiceProvider
{
    // ...

    public function boot(): void
    {
        Password::defaults(function () {
            return Password::min(8)
                ->mixedCase()
                ->numbers();
        });
    }
}
use Illuminate\Contracts\View\View;
use Illuminate\Validation\Rules\Password;
use Livewire\Component;

class Profile extends Component
{
    // ...

    public function render(): View
    {
        return view('livewire.profile', [
            'appliedRules' => Password::default()->appliedRules(),
        ]);
    }

    public function save(): void
    {
        // ...
    }
}
<div>
    <form wire:submit="save">
        <div>
            <x-input label="Password" />

            <span>{{ __('The password must be at least :min characters long.', ['min' => $appliedRules['min']]) }}</span>

            @if ($appliedRules['mixedCase'] === true)
                <span>{{ __('The password must contain at least one uppercase and one lowercase letter.') }}</span>
            @endif

            @if ($appliedRules['numbers'] === true)
                <span>{{ __('The password must contain at least one number.') }}</span>
            @endif
        </div>
        <x-button type="submit">
            Save
        </x-button>
    </form>
</div>

@devajmeireles devajmeireles changed the title [12.x] Introducing Rules\Password::acceptedRules Method [12.x] Introducing Rules\Password::appliedRules Method Mar 30, 2025
@utsavsomaiya
Copy link
Contributor

        @if ($appliedRules['mixedCase'] === true)
            <span>{{ __('The password must contain at least one uppercase and one lowercase letter.') }}</span>
        @endif

        @if ($appliedRules['numbers'] === true)
            <span>{{ __('The password must contain at least one number.') }}</span>
        @endif

May be this would be always based on js framework like alpine?

@devajmeireles
Copy link
Contributor Author

        @if ($appliedRules['mixedCase'] === true)
            <span>{{ __('The password must contain at least one uppercase and one lowercase letter.') }}</span>
        @endif

        @if ($appliedRules['numbers'] === true)
            <span>{{ __('The password must contain at least one number.') }}</span>
        @endif

May be this would be always based on js framework like alpine?

I believe that regardless of the type of frontend, appliedRules will be useful.

Two examples:

  1. Imagine a NuxtJS-based frontend consumes your Laravel API. We can use Password::appliedRules to pass the expected password format to NuxtJS so that NuxtJS prints it gracefully.
  2. FluxUI may have a form-specific password component that displays the expected password format at any time, so again, Password::appliedRules will be useful to match the password format between the frontend and the backend.

@utsavsomaiya
Copy link
Contributor

Yeah but it will remove or changing the colors based on the input! May be yeah you're right idk!

@taylorotwell taylorotwell merged commit 19fc5b2 into laravel:12.x Apr 1, 2025
33 of 34 checks passed
@devajmeireles devajmeireles deleted the introducing-password-method-to-get-rules-applied branch April 2, 2025 00:26
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.

3 participants