Laravel 8.x Robust Password Validation Rule Object

tutspack laravel
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello artisans,

How are you doing? Do you know, on April 27th, 2021 Laravel has released a new Password Validation Rule object. The Password validation rule object has a fluent API for common password requirements as well as compromised passwords.

Strong Password Validation Rule Object

If you don’t know how to use the Password validation rule object in your form request, don’t worry I will show you step by step. I assume you have a Laravel Project which has a form.

Inside the form handler method you have to use like bellow .

 public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
        //  'password' => 'required|string|confirmed|min:8',
            'password' => ['required', 'confirmed', Password::min(8)],
        ]);
}

In addition, this new custom rule object contains built-in methods that you may use to ensure strong passwords. you can use other methods for strong validation.

Password Validation Rule Object

  $request->validate([

            // Makes the password require at least one uppercase and one lowercase letter.
            'password' =>  ['required', 'confirmed', Password::min(8)->mixedCase()],

             // Makes the password require at least one letter.
            'password' =>  ['required', 'confirmed', Password::min(8)->letters()],

            // Makes the password require at least one number.
            'password' =>  ['required', 'confirmed', Password::min(8)->numbers()],

            // Makes the password require at least one symbol.
            'password' =>  ['required', 'confirmed', Password::min(8)->symbols()],

            // Ensures the password has not been compromised in data leaks.
            'password' =>  ['required', 'confirmed', Password::min(8)->uncompromised()],
        ]);

You may think, It’s a boring task that repeated the same task fluently. Don’t worry you may define password validation rules like below.

 public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => ['required', 'confirmed', Password::min(8)
                    ->mixedCase()
                    ->letters()
                    ->numbers()
                    ->symbols()
                    ->uncompromised(),
            ],
        ]);
}

Read More : Send SMS in mobile using nexmo from laravel application

Read More : 10 PHP Interview Questions You should know

Hope you will enjoy this tutorial.


Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published. Required fields are marked *