Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 防止覆盖页面上的错误消息_Laravel_Blade_Laravel 5.4 - Fatal编程技术网

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谢谢,但由于字段名称与数据库中的列相同,因此我无法更改字段名称。谢谢,但我使用
请求
,而不是在控制器中验证