2
想檢查我已經做了一個CustomValidator.php處理所有我的額外驗證規則,但問題是我應該如何返回自定義錯誤消息?這是我做我的CustomValidator.php文件,Laravel 5自定義驗證規則消息錯誤
<?php namespace App\Validators\CustomValidator;
use Illuminate\Validation\Validator;
use Auth;
class CustomValidator extends Validator
{
public function validateVerifyPassword($attribute, $value, $parameters)
{
$currentUser = Auth::user();
$credentials = array ('email' => $currentUser->email, 'password' => $currentUser->password);
return Auth::validate($credentials);
}
protected function replaceVerifyPassword($message, $attribute, $rule, $parameters)
{
return str_replace($attribute, $parameters[0], $message);
}
}
,這是我在如何定義FormRequest.php我的自定義錯誤消息
public function messages()
{
return [
'login_email.required' => 'Email cannot be blank',
'old_password.required' => 'You need to provide your current password',
'old_password.between' => 'Your current password must be between :min and :max characters',
'old_password.verifyPassword' => 'Invalid password',
'password.required' => 'Password is required.',
'password.between' => 'Your password must be between :min and :max characters',
'password_confirmation.required' => 'You need to retype your password',
'password_confirmation.same' => 'Your new password input do not match',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
注意的是,驗證部分工作,只有它不會通過自定義錯誤消息,並且它與
CustomValidator.php line 18:
Undefined offset: 0
一個錯誤是在$parameter[0]
部分還給我