I am using Laravel 5.3. I have a form which is not filling the fields with old input when validation fails. The form looks like this:
{{ Form::open(['route' => 'my.route', 'class' => 'form-horizontal', 'files' => true]) }}
<div class="form-group">
{{ Form::label('name', 'Name', ['class' => 'control-label col-md-3']) }}
<div class="col-md-9">
{{ Form::text('name', null, ['placeholder' => 'hello world' ,'class' => 'form-control']) }}
</div>
</div>
<div class="form-group">
{{ Form::label('description', 'Description', ['class' => 'control-label col-md-3']) }}
<div class="col-md-9">
{{ Form::textarea('description', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
<span>some sub-text</span>
</div>
</div>
<div class="form-group">
{{ Form::label('date', 'Date', ['class' => 'control-label col-md-3']) }}
<div class="col-md-9">
{{ Form::text('date', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
</div>
</div>
{{ Form::close() }}
I am processing the information like this:
public function store(ItemRequest $request, ImageMagick $imageMagick)
{
$item = new Item;
$item->name = $request->name;
$item->description = $request->description;
$item->date = $request->date;
$item->save();
return redirect()->route('some.other.route');
}
I am doing my validation in my own form request file "ItemRequest" which looks like this:
class ItemRequest extends FormRequest
{
public static $rules = [
'name' => 'required|min:5|max:255',
'description' => 'required|min:50',
'date' => 'required'
];
/**
* 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()
{
if ($this->isMethod('post')) {
return $this->createRules();
}
return $this->updateRules();
}
public function createRules()
{
return self::$rules;
}
public function updateRules()
{
return array_merge(
self::$rules,
$this->otherRules()
);
}
public function otherRules()
{
return ['age' => "required"];
}
}
I think the problem lies in ItemRequest. I have tried adding ->withInput() to my return in the store method but the old inputs are still not being retained.
return redirect()->route('some.other.route')->withInput();
But it did not make a change.
EDIT: I was clearing the old input when the page refreshed.