Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

php - Laravel 5.2 $errors not appearing in Blade

So I'm following along with the Laravel 5 fundamentals tutorial and I am stuck on the form validation. I have followed along exactly with the tutorial but I am getting a Undefined variable: errors in my create articles view.

In the tutorial I am following and what I have found online they say the errors variable is always there in the blade file for you to use so I don't know what i am doing wrong?

Any help would be appreciated! loving Laravel except for this error!

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

Controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppUser;
use AppHttpRequests;
use AppHttpRequestsUserRequest as UserRequest;
// use AppHttpRequestsCreateArticleRequest as CreateArticleRequest;
use AppHttpControllersController;
use IlluminateViewMiddlewareErrorBinder;

class UserController extends Controller
{
    public function create(){
      return view('pages.signUp');
    }

    public function store(UserRequest $request){
      User::create($request->all());
      return 'the user has been registered!';
      return view('user.profile');
    }

}

Request validation

<?php

namespace AppHttpRequests;

use AppHttpRequestsRequest;

class UserRequest extends Request
{
    /**
     * 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 [
            'name' => 'required',
            'email' => 'required|email',
            'country' => 'required',
            'password' => 'required|min:6',
            'confirm_password' => 'required|same:password',
            'height' => 'required',
            'weight' => 'required',
        ];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.

There are two ways to fix this:

  1. In your kernel.php file, you can move the middleware IlluminateViewMiddlewareShareErrorsFromSession::class back to the protected $middleware property.

  2. You can wrap all your web routes with a route group and apply the web middleware to them.

    Route::group(['middleware' => 'web'], function() {
        // Place all your web routes here...
    });
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...