我有一个自定义规则来检查密码要求。如果验证失败,我想返回消息。
但是我想将所有验证失败的数组返回给我的VueJS SPA。
当前,当我输出错误消息时,我只是得到“ password
”
如何获取一系列验证消息?还是应该将每个验证规则(长度,大写等。)都放在其自己的Rule类中?
public function expired(PasswordChangeRequest $request) {
// do on form submission
}
class PasswordRequirement implements Rule
{
private $error_msgs = [];
public function __construct()
{
//
}
public function passes($attribute, $value)
{
if (empty($value)) {
$this->error_msgs .= ':attribute is required.';
}
if (strlen($value) < 6 || strlen($value) > 10) {
$this->error_msgs .= 'Password must be between 6 and 10 characters.';
}
if (!preg_match('/[0-9]/', $value)) {
$this->error_msgs .= 'Password must contain at least one number.';
}
if (!preg_match('/[A-Z]/', $value)) {
$this->error_msgs .= 'Password must contain at least one uppercase letter.';
}
if (!preg_match('/[a-z]/', $value)) {
$this->error_msgs .= 'Password must contain at least one lowercase letter.';
}
return !(count($this->error_msgs) > 0);
}
public function message()
{
return $this->error_msgs;
}
}
class PasswordChangeRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'password' => new PasswordRequirement(),
];
}
public function messages()
{
return ['password'];
}
}