Reputation: 9314
What I've done is pretty basic. I can see my validation errors before redirecting so I would presume I've either overwritten the session (which I'm not doing) or removing something from the views which also doesn't make sense as the validator works for my \Auth\register but none other action.
This is what I have:
in \App\Controllers\QuestionsController
QuestionsController extends Controller{
protected$rules=[
'question'=>array(
'text'=>'required|max:2000',
'categories'=>'required|min:4'
)
];
public function create(Request $request){
$valid=Validator::make($request->all(),$this->rules['question']);
if($valid->fails()){
var_dump($valid->messages());//exit;
return redirect('/questions')->withErrors($valid,'questions');
}
//...additional adding scripts etc...
}
Now this is where before redirecting I can see my messages!
Now you'd think I was being silly and not using a name bag or something but even if I remove the name bag I see no messages :S
In my question.add view
@import('defaults.errors',['sub'=>'questions'])
{{var_dump(count($errors))}}
{{var_dump($errors->questions)}}
in defaults.errors
@if(isset($sub))
@if(count($errors->$sub)>0)
<div class="alert alert-danger">
<ul class='nav'>
@foreach($errors->$sub->all() as $error)
<li><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@else
@if(count($errors)>0)
<div class="alert alert-danger">
<ul class='nav'>
@foreach($errors->all() as $error)
<li><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@endif
I think maybe because I don't understand Laravel all that well maybe I've 'overwritten' something to do with the $errors or session in my Controller?
This is what my Controller looks like
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Auth;
use App\conversation;
use App\notifications;
use App\relationships;
use App\Helpers\DatetimeHelper;
use Azza\Utilities\NumberSuffix;
class Controller extends BaseController
{
public $user;
public $title=array('title');
public $titleSeperator=' - ';
public $keywords=array('hello','I added');
public $messageCount=0;
public$notifications=null;
public$notificationsCount=0;
public$friendRequests=0;
public$bodyClass=array();
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
public function __construct(){
$this->user=Auth::user();
if($this->user){
$this->messageCount=conversation::getByUserId($this->user->id)->countUnseen()->count();
//$this->notifications=notifications::getByUserId($this->user->id)->get()->paginate(5);
$this->notificationsCount=notifications::getByUserId($this->user->id)->countUnseen()->count();
//var_dump($this->messageCount->count());exit;
$this->friendRequests=relationships::countRequests($this->user->id)->count();
}
view()->composer('*',function($view){
$view->with('currentUser',$this->user);
$view->with('title',$this->title);
$view->with('titleSeperator',$this->titleSeperator);
$view->with('keywords',$this->keywords);
$view->with('messageCount',$this->messageCount);
$view->with('notificationsCount',$this->notificationsCount);
$view->with('friendRequests',$this->friendRequests);
$view->with('DatetimeHelper',new DatetimeHelper());
$view->with('NumberSuffix',new NumberSuffix());
$view->with('bodyClass',implode(' ',$this->bodyClass));
});
}
public function setDefaultVals($name,$val){
view()->composer('*',function($view) use($name,$val){
$view->with($name,$val);
});
}
}
The basic idea of doing this was to just free up time manipulating my header information and using dynamic arrays for data. I know this looks messy and probably not the best way but I'm only messing about with laravel at the moment.
Upvotes: 0
Views: 122
Reputation: 2506
I've set up your code in my Laravel environment, everything you're doing is correct. However, in 5.2
laravel's middleware acts a little differently, your $errors
variable is only available when you have the StartSession
middleware active.
Your Kernel.php will look like this, or similar:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
Try changing it to this:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
Notice the switch-a-roo on the \Illuminate\Session\Middleware\StartSession::class
Upvotes: 1