mercredi 23 décembre 2015

Laravel reset value in input after validation fail?

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire