Laravel 5重定向后未显示验证器错误消息

时间:2019-01-25 10:04:40

标签: laravel

错误消息未显示。我在   sendFailedLoginResponse,它将重定向到登录页面,而不会显示错误消息

Latest Value =
VAR 
   Latest_Date = CALCULATE ( MAX ( Users[AddedDate] ), ALL ( Users ) )
RETURN 
   CALCULATE ( SUM ( Users[Value] ), Users[AddedDate] = Latest_Date )

刀片

protected function sendFailedLoginResponse(Request $request)
{
     return redirect()->route("login")->withErrors([
            $this->username() => [trans('auth.failed')],
        ]);

}

4 个答案:

答案 0 :(得分:1)

return redirect()-> route(“ login”)-> withErrors(['email'=> trans('auth.failed')]);

答案 1 :(得分:0)

像这样传递消息包对象而不是数组。

-o

答案 2 :(得分:0)

输入字段的名称应为withErrors()函数的第二个参数。
Laravel documentation - Manually Creating Validators

protected function sendFailedLoginResponse(Request $request)
{
     return redirect()->route("login")->withErrors(trans('auth.failed'), 'login');
}

刀片文件

<div class="form-group col-md-12">
<input id="email" name="email" class="" type="email" placeholder="Your Email">
@if ($errors->has('email'))
    <span class="invalid-feedback" role="alert">
        <strong>{{ $errors->login->first('email') }}</strong>
    </span>
@endif

答案 3 :(得分:0)

如果您的应用程序太大,请按照以下步骤操作

在进行验证时,有几种方法

方法一

Using Validator Facade

public function store(Request $request)
    {
        $input = $request->all();
        $validator = \Validator::make($input, [
            'post_name' => 'required',
            'post_type' => 'required',
        ]);

        if ($validator->fails()) {
                  return redirect()->back()
                                  ->withErrors($validator)
                                  ->withInput($input);
        }    
        Post::create($input);
        return redirect('post.index');
    }

方法二

using $this->validate(); Method

public function store(Request $request)
    {
        $this->validate($request, [
            'post_name' => 'required',
            'post_type' => 'required',
        ]);

        Post::create($request->all());       
    }

方法三

使用请求方法

php artisan make:request PostStoreRequest

此文件将在app\Http\Requests中创建,其名称为PostStoreRequest.php

打开控制器并添加

use App\Http\Requests\PostStoreRequest;

现在文件内容

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;


class PostStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'post_name' => 'required',
            'post_type' => 'required',
        ];
    }

    /**
     * Custom message for validation
     *
     * @return array
     */
    public function messages()
    {
        return [
            'post_name.required' =>'Enter Post Name',
            'post_type.required' =>'Enter Post Type',

        ];
    }

}

如果要自定义错误消息,请使用messages函数

现在的商店功能

public function store(PostStoreRequest $request)
    { 

        Post::create($request->all() );
        return redirect()->route('post.index')->with('success','CrmHfcImageGallery Created Successfully');
    }

现在进入视图

要查看所有消息,请将其添加到刀片文件顶部

@if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif

查看特定消息

<div class="col-sm-4">
         <div class="form-group @if ($errors->has('post_name')) has-error @endif">
            {!! Form::label('post_name','Post Name') !!}
            {!! Form::text('post_name',old('post_name'),['placeholder'=>'Enter Post Name ','class' =>'form-control rounded','id' =>'post_name']) !!}
            @if ($errors->has('post_name'))
            <p class="help-block">{{ $errors->first('post_name') }}</p>
            @endif
         </div>
      </div>

希望有帮助