Laravel 防止覆盖页面上的错误消息
我有一个页面,有两个单独的表单、路由和请求 在此表单中,我有一个同名字段(例如,在每个表单中,我都有Laravel 防止覆盖页面上的错误消息,laravel,blade,laravel-5.4,Laravel,Blade,Laravel 5.4,我有一个页面,有两个单独的表单、路由和请求 在此表单中,我有一个同名字段(例如,在每个表单中,我都有emailfield) 问题是当我验证一个表单时,当表单有错误时,在两个表单中都会显示错误 如何防止覆盖错误 这是我的密码: 注册请求: class RegisterFormRequest extends FormRequest { /** * Get the validation rules that apply to the request. * * @r
email
field)
问题是当我验证一个表单时,当表单有错误时,在两个表单中都会显示错误
如何防止覆盖错误
这是我的密码:
注册请求:
class RegisterFormRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email|unique:users',
'name' => 'required',
'location' => 'required'
];
}
}
class LoginFormRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email',
'password' => 'required'
];
}
}
登录请求:
class RegisterFormRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email|unique:users',
'name' => 'required',
'location' => 'required'
];
}
}
class LoginFormRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email',
'password' => 'required'
];
}
}
在blade中,我以两种形式使用上述电子邮件字段代码:
{{$errors->has('email') ? 'has-error' : null }}
当我没有在登录表单中输入电子邮件并提交时,错误也会显示在注册表单的电子邮件字段上方。我有一个解决方案,但我不确定这是否是最好的方法。登录控制器使用
AuthenticatesUsers
特征,该特征包含一个方法sendFailedLoginResponse
通过在LoginController
中重写此方法,可以将错误消息包命名为:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors, 'login');
}
@if (! $errors->login->isEmpty())
<ul>
@foreach ($errors->login->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
然后,在刀片文件中,您可以执行以下操作:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors, 'login');
}
@if (! $errors->login->isEmpty())
<ul>
@foreach ($errors->login->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
@if(!$errors->login->isEmpty())
@foreach($errors->login->all()作为$error)
- {{$error}}
@endforeach
@恩迪夫
我终于找到了解决方案
我应该在请求
文件中添加$errorBag
变量:
class RegisterFormRequest extends FormRequest
{
protected $errorBag = 'register';
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email|unique:users',
'name' => 'required',
'location' => 'required'
];
}
}
在刀片中:
{{$errors->register->has('email') ? 'has-error' : null }}
也许您可以将字段电子邮件的名称更改为其他名称?@Mozammilkhodabackhas谢谢,但由于字段名称与数据库中的列相同,因此我无法更改字段名称。谢谢,但我使用
请求
,而不是在控制器中验证