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

1
  • Can you show us your validate function? Commented Dec 23, 2015 at 20:31

4 Answers 4

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'))
Sign up to request clarification or add additional context in comments.

2 Comments

It does not work after adding ->withInput() method
works fine, the fields doesnt reset if any validation error. checked on Laravel 5.5
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.

Comments

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

3 Comments

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
In that case, it would possible he was using Form::model() where I believe that would actually fill in those values.
Problem may cause by the new LaravelCollective Html has replace for Illuminate Html
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.