php artisan make:rule IsValidPassword
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;
class IsValidPassword implements Rule
{
/**
* Determine if the Length Validation Rule passes.
*
* @var boolean
*/
public bool $lengthPasses = true;
/**
* Determine if the Uppercase Validation Rule passes.
*
* @var boolean
*/
public bool $uppercasePasses = true;
/**
* Determine if the Numeric Validation Rule passes.
*
* @var boolean
*/
public bool $numericPasses = true;
/**
* Determine if the Special Character Validation Rule passes.
*
* @var boolean
*/
public bool $specialCharacterPasses = true;
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->lengthPasses = (Str::length($value) >= 8);
$this->uppercasePasses = (Str::lower($value) !== $value);
$this->numericPasses = ((bool) preg_match('/[0-9]/', $value));
$this->specialCharacterPasses = ((bool) preg_match('/[^A-Za-z0-9]/', $value));
return ($this->lengthPasses && $this->uppercasePasses && $this->numericPasses && $this->specialCharacterPasses);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
if (!$this->lengthPasses)
{
return 'The :attribute must be at least 8 characters.';
}
elseif (!$this->uppercasePasses)
{
return 'The :attribute must contain at least one uppercase character.';
}
elseif (!$this->numericPasses)
{
return 'The :attribute must contain at least one number.';
}
else
{
return 'The :attribute must contain one special character.';
}
}
}
$request->validate([
'password' => ['required','max:255',new isValidPassword(),'required_with:password_confirmation','same:password_confirmation'],
'password_confirmation' => 'required|min:8'
]);
Digər dildə:
EN