dimanche 13 mars 2016

laravel validator not show errors

i am new to laravel and other php frameworks.

Try simple form and validating. Like examples in http://ift.tt/1USwF7I

routes.php


 Route::get('/post/', 'PostController@create');
 Route::post('/post/store', 'PostController@store');

create.blade.php

<html>
<head>
    <title>Post form</title>

</head>
<body>
 <h1>Create Post</h1>
    <form action="/post/store" method="POST">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="form-group">
            <label for="title">Title</label>   

            <input type="text" id="title"  name='title'>
          </div>
          <button type="submit" class="btn btn-default">Save</button>
    </form>
</body>

PostController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use View;
use Validator;

class PostController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return Response
     */
    public function create()
    {
        return view('post.create');
    }

    /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        // Validate and store the blog post...

        $validator = Validator::make($request->all(), [
            'title' => 'required|min:5'
        ]);

        if ($validator->fails()) {

            dd($validator->errors);
            //return redirect('post')
                        //->withErrors($validator)
                        //->withInput();
        }
    }
}

When i post not valid data:

ErrorException in PostController.php line 37: Undefined property: Illuminate\Validation\Validator::$errors

Validator object nor have errors.

If enabled in controller

return redirect('post')->withErrors($validator)
                   ->withInput();

and enabled in form

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Have error

ErrorException in c5df03aa6445eda15ddf9d4b3d08e7882dfe13e1.php line 1: Undefined variable: errors (View: /www/alexey-laravel-1/resources/views/post/create.blade.php)

This error in default get request to form and after redirect from validator



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire