i use the validated method for validate request, but the var errors is empty in the view :/.
in the controller, i have:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function(){
return view('home');
})->name('home');
Route::group(['prefix' => 'do'], function($name = null){
Route::get('/{action}/{name?}', ['uses' => 'controllers@get', 'as' => 'get']);
Route::post('/', ['uses' => 'controllers@post', 'as' => 'post' ]);
});
});
for the controller, i have:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class controllers extends Controller
{
public function get($action, $name=null)
{
return view('actions.' . $action, ['name' => $name]);
}
public function post(Request $request)
{
$this->validate($request, [
'action' => 'required',
'name' => 'alpha|required'
]);
return view('actions.'.$request['action'] , ['action' => $request['action'], 'name'=>$this->transformName($request['name'])]);
}
private function transformName($name)
{
$add = "king ";
return $add.strtoupper($name);
}
}
and for the view, i have:
@extends('layouts.master')
@section('content')
<div class="centered">
<a href="{{route('get', ['action' => 'greet'])}}">greet</a>
<a href="{{route('get', ['action' => 'hug'])}}">hug</a>
<a href="{{route('get', ['action' => 'kiss'])}}">kiss</a>
<br >
<br >
@if (isset($errors) && count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
{!! $errors->first() !!}
@endforeach
</ul>
</div>
@endif
<form action="{{ route('post')}}" method="post">
<label for="select-action">I want to...</label>
<select id="select-action" name="action">
<option value="greet">greet</option>
<option value="hug">hug</option>
<option value="kiss">kiss</option>
</select>
<input type="text" name="name">
<button type="submit">action</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
@endsection
if i delete the middleware of the route, the var errors work correctly.
how i can fix this?
thanks.