Laravel reset value in input after validation fail?
Solution 1
return the following from your controller...
return redirect('articles')->withInput();
see if that helps. you can exclude certain fields if you wanted to like this..
return redirect('articles')->withInput($request->only('email'))
Solution 2
There are a couple of issues here.
The first one, the inputs not being saved after validation fails. I believe this is because you are passing null
into the functions which are building the inputs when you should be passing in the value you wish it to default to. Try the following...
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>
This should then populate the input element with the old data which was previously entered. Just follow the same procedure for the rest of the form inputs.
The second issue with the $errors
value not being set is actually due to a change with Laravel 5.2. Many people have been having the same exact issue so I'd just like to refer you to a previous answer: Laravel 5.2 $errors not appearing in Blade
Solution 3
If your 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' the value with the old data.
Solution 4
Best way for you
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="upload_title" class="form-control" value="{{ (old('title')) ? old('title') : $data->title}}">
</div>
Kieu Duy
Updated on June 25, 2022Comments
-
Kieu Duy almost 2 years
I'm so confuse now, I'm learning Laravel from Laracast, according to the instructor, after validation fail, the form does not reset values that user entered. But when testing validation, when I submit the form reset every thing.
Another question is the undefined variable
$errors
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('Y-m-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
He use v5.0 and I'm using v5.2
-
Kieu Duy over 8 yearsIt does not work after adding
->withInput()
method -
Kieu Duy over 8 yearsI'll test again with 5.0, I watched the video again and he did not fill function
old('title')
, justnull
. Thank for your answer -
user1669496 over 8 yearsIn that case, it would possible he was using
Form::model()
where I believe that would actually fill in those values. -
Kieu Duy over 8 yearsProblem may cause by the new LaravelCollective Html has replace for Illuminate Html
-
Hashaam Ahmed over 5 yearsworks fine, the fields doesnt reset if any validation error. checked on Laravel 5.5