Laravel 5.2 validation errors not appearing

后端 未结 4 1183
名媛妹妹
名媛妹妹 2021-01-21 13:13

I am trying to get validation errors to show up in Laravel.

I have a UserController set up like so:



        
相关标签:
4条回答
  • 2021-01-21 13:21
    @if(count(($errors->all()) > 0))
    @foreach($errors->all() as $error)
        <div class="alert alert-danger">
            {{$error}}
        </div>
    @endforeach
    @endif
    
    0 讨论(0)
  • 2021-01-21 13:24

    The errors block should be:

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
            </ul>
        </div>
    @endif
    

    Assuming you're using Bootstrap for the alerts.

    You also don't have $validator defined. You need to do something like this:

    $validator = Validator::make($request->all(), [
        'email' => 'required|unique:users|email|max:255',
    ]);
    

    Instead of $this->validate().

    That should do it.

    0 讨论(0)
  • 2021-01-21 13:40

    In laravel version 5.2.41, the middleware web is thrown out.

    Means adding the routes inside Route::group(['middleware' => ['web']], function () { will make the validation not work.

    0 讨论(0)
  • 2021-01-21 13:45

    There are a couple things wrong or that can be improved here. The store method on the UserController has a lot of weird issues. $this will always be true because objects are true in php. Also, you pass in $validator into withErrors which doesn't make sense because there's no variable validator.

    public function store(Request $request) {
        $this->validate($request, [
            'email' => 'required|unique:users|email|max:255',
        ]);
    
        User::create(Request::all());
        return redirect('/');
    }
    

    The validate method will throw an Illuminate\Foundation\Validation\ValidationException if there is a validation error. This exception should be listed in the $dontReport instance variable in App\Exceptions\Handler as seen below:

    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];
    

    If you have changed these values, removed, or modified the ValidatesRequest trait you may have broken this functionality.

    Your error reporting code is not correct either:

    @foreach ($errors->all() as $error)
       {!! $errors->first() !!}
    @endforeach
    

    There are 3 changes here. First I removed the outer errors size check, this doesn't really get you anything. Next, I fixed your extra } error, the syntax for un-escaping data is {!! $errors->first() !!}. Lastly, I called ->first() this returns the first error associated with that particular field.

    I think it's important to note that the validation exception will create a redirect response to the previous page. The logic for determining the previous page can be found in Illuminate\Routing\UrlGenerator::previous().

    0 讨论(0)
提交回复
热议问题