There is such a controller code

public function add(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required' ]); return view('cars_add',[ 'validator' => $validator ]); } 

and such html

  <div class="item"> <label>Заголовок</label> <input type="text" name="title"> <?php if($errors->has('title')): ?> <span class="error"><?php echo $errors->first('title'); ?></span> <?php endif; ?> </div> 

Why does the error information when an empty field does not come out, what's the problem?

  • What is the version of Laravel? In the question indicate - VenZell

2 answers 2

You do not pass errors to the template. Here is an example of a validator running. Documentation

ps I ask to pay attention that the validator changed throughout versions. Example for 5.2

 class PostController extends Controller { /** * Store a new blog post. * * @param Request $request * @return Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); if ($validator->fails()) { return redirect('post/create') ->withErrors($validator) ->withInput(); } // Store the blog post... } } 

    Notice that you pass the $validator variable to the template, not $errors in this section of code:

     return view('cars_add',[ 'validator' => $validator ]); 

    Correct the variable declaration:

     public function add(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required' ]); return view('cars_add',[ 'errors' => $validator ]); }