jeudi 1 novembre 2018

Laravel 5.7.x Validator Errors not showing in view

I know this question has been asked about earlier versions of Laravel, but I"m using 5.7.x (the latest) and what worked in 5.2 might not be applicable in my case. Basically, I'm trying to create a Post form validated by a custom FormRequest. Here are my source files.

post.create.blade.php

<html>
    @include('header')
    <body>
        <h1>Add a New Post</h1>

        {!! Form::open(['route' => 'save_post']) !!}
            <div class="form-group">
                {!! Form::label('name', 'Name:') !!}
                {!! Form::text('name', null) !!}
            </div>
            <div class="form-group">
                {!! Form::label('body', 'Body:') !!}
                {!! Form::textarea('body', null) !!}
            </div>
            {!! Form::submit('Create', ['class' => 'btn btn-info']) !!}
        {!! Form::close() !!}

        <div class="alert alert-danger">
            <ul>
                @if($errors->any())
                    @foreach ($errors->all() as $error)
                        <li></li>
                    @endforeach
                @endif
            </ul>
        </div>
    </body>
</html>

web.php

Route::get('post/create', function () {
return view('post_create');
});

PostController.php

<?php
namespace App\Http\Controllers;
use App\Film;
use App\Http\Requests\FilmCreateRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
    public function index()
    {
        return Post::paginate(1);
    }

    public function show($id)
    {
        return Post::find($id);
    }

    public function store(PostCreateRequest $request)
    {        
        $post = Post::create($request->all());
        $post->save();

        return response()->json($post, 201);
    }

    public function update(Request $request, $id)
    {
        $post = Post::findOrFail($id);
        $post->update($request->all());
        return response()->json($post, 200);
    }

    public function delete(Request $request, $id)
    {
        $post = Post::findOrFail($id);
        $post->delete();
        return response()->json(null, 204);
    }

PostCreateRequest.php

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Foundation\Validation\ValidatesRequests;
class PostCreateRequest extends FormRequest
{
    /**
     * 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()
    {
        return [
            'name' => 'required|string|max:255',
            'body' => 'required|string|max:4096'
        ];
    }
}

Clearly the validator is working. When I fill out the name and body, it adds a post to the SQL database on the backend. And when it fails, it goes back to the post create view. The problem is that the validator errors don't show up in the view even though I have it coded in. What exactly is going on? Is there some sort of bug in Laravel?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire