Laravel reset value in input after check failed?

I am so confused, I am learning Laravel from Laracast, according to the instructor, after the verification failed, the form did not enter reset the values ​​entered by the user. But when checking validation, when I submit the reset form every thing.

Another question is the undefined $errors variable when I try to access it.

My controller

 <?php namespace App\Http\Controllers; use App\Articles; use App\Http\Requests\CreateArticle; use Carbon\Carbon; use App\Http\Requests; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class ArticlesController extends Controller { public function create() { return view('articles.create'); } public function store(Request $request) { $this->validate($request, [ 'title' => 'required', 'body' => 'required' ]); Articles::create($request->all()); return redirect('articles'); } } 

My view

 @extends('app') @section('content') <h1>Create a new Articles</h1> <hr/> {!! Form::open(['url' => 'articles']) !!} <div class="form-group"> {!! Form::label('title', 'Title: ') !!} {!! Form::text('title', null, ['class' => 'form-control']) !!} </div> <div class="form-group"> {!! Form::label('body', 'Body') !!} {!! Form::textarea('body', null, ['class' => 'form-control']) !!} </div> <div class="form-group"> {!! Form::label('published_at', 'Published On:') !!} {!! Form::input('text', 'published_at', date('Ym-d'), ['class' => 'form-control']) !!} </div> <div class="form-group"> {!! Form::submit('submit', ['class' => 'btn btn-primary']) !!} </div> @if(isset($errors)) {{var_dump($errors)}} @endif {!! Form::close() !!} @stop 

It uses v5.0 and I use v5.2

+5
source share
3 answers

There are several issues here.

First, inputs that are not saved after validation fail. I believe this happens because you pass null to functions that build inputs when you must pass the value you want by default. Try the following ...

 <div class="form-group"> {!! Form::label('title', 'Title: ') !!} {!! Form::text('title', old('title'), ['class' => 'form-control']) !!} </div> 

Then it should fill the input element with the old data that was previously entered. Just follow the same procedure for the rest of the form inputs.

The second issue with the $errors value that is not set is actually related to the change from Laravel 5.2. Many people had the exact same problem, so I would like to give you the previous answer: Larvel 5.2 $ errors not appearing in Blade

+4
source

If you are not using

 <div class="form-group"> {!! Form::label('title', 'Title: ') !!} {!! Form::text('title', old('title'), ['class' => 'form-control']) !!} </div> 

but have simple input fields like

 <input id="Email" name="Email" type="email" class="uit-input" placeholder="your email address" value={{old('Email')}}> 

'prefill' value with old data.

+4
source

return the following from your controller ...

 return redirect('articles')->withInput(); 

see if that helps. You can exclude certain fields if you wish.

 return redirect('articles')->withInput($request->only('email')) 
+3
source

Source: https://habr.com/ru/post/1239086/


All Articles