建立配置檔案
config/password.php
<?php
return [
/**
* 最小長度
*/
'minimum_length' => env('PASSWORD_LENGTH', 8),
/**
* 密碼要求
*/
'requirements' => [
'[0-9]',
'[a-z]',
'[A-Z]',
'[!@#$%^&*()]',
],
];
編寫你的rule 檢視相關文件la ravel
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'email' => [
'required',
'email'
],
'password' => array_merge(
[
'required',
'between:' . config('password.minimum_length', 8) . ',20'
],
array_map(function($requirement) {
return function($attribute, $value, $fail) use ($requirement) {
if (! (bool) preg_match('/' . $requirement . '/', $value)) {
$fail(Lang::get('password.password_requirements.' . $requirement));
}
};
},
config('password.requirements', [
'[0-9]',
'[a-z]',
'[A-Z]',
]))
),
];
}
為你準備好的語言包
resources/lang/zh-cn/passowrd.php
return [
'password_requirements' => [
'[0-9]' => '密碼必須至少包含一個整數。',
'[a-z]' => '密碼必須至少包含一個小寫字母。',
'[A-Z]' => '密碼必須至少包含一個大寫字母。',
'[!@#$%^&*()]' => '密碼必須至少包含一個特殊字元。',
]
]