8

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

Black
  • 18,150
  • 39
  • 158
  • 271
Kieu Duy
  • 429
  • 1
  • 4
  • 12

4 Answers4

5

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'))
nrivero
  • 281
  • 1
  • 9
5

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.

Marco
  • 319
  • 4
  • 13
4

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

Community
  • 1
  • 1
user1669496
  • 32,176
  • 9
  • 73
  • 65
  • I'll test again with 5.0, I watched the video again and he did not fill function `old('title')`, just `null`. Thank for your answer – Kieu Duy Dec 23 '15 at 21:21
  • 1
    In that case, it would possible he was using `Form::model()` where I believe that would actually fill in those values. – user1669496 Dec 23 '15 at 21:25
  • Problem may cause by the new LaravelCollective Html has replace for Illuminate Html – Kieu Duy Dec 23 '15 at 21:32
2

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>
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
KEYDUC
  • 21
  • 1