4
votes

I've got a new site running Laravel 5.2 (Laravel Framework version 5.2.39). NOTE: the routes file is NOT using the web middleware group, which is no longer needed and can cause this issue.

I've got a simple validation on the ContactController's store method:

    $this->validate($request, [
        'ContactFirst' => 'required|max:25',
        'ContactLast' => 'required|max:25',
        'ContactRole' => 'required|max:25',
        'ContactEmail' => 'email|max:255',
        'ContactPhone' => 'max:255',
    ]);

When I intentionally fail the validation, the site redirects back to the form, but the error bag is empty so no error info is provided.

In the form view (resources/contacts/new.blade.php) I put the following code from the docs as well as a dump:

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

The page (as I said) redirects back to the form and the inputs are populated. But $errors is empty and no messages are printed:

object(Illuminate\Support\ViewErrorBag)[285]
protected 'bags' => 
array (size=0)
  empty
3
Are you sure that you're not using the web middleware (I think that's the one you meant in the beginning of your question). - resmall
@TiagoRL-- yes, the web middleware is not being applied in the routes.php - user101289

3 Answers

3
votes

Actually I don't know if Your code is possible, because there is not exact code that shows that it will return back with errors.

I do use validation checking with $validator->fails().

Check this example:

$validator = Validator::make($request->all(), [
    'ContactFirst' => 'required|max:25',
    'ContactLast' => 'required|max:25',
    'ContactRole' => 'required|max:25',
    'ContactEmail' => 'email|max:255',
    'ContactPhone' => 'max:255',
]);

if ($validator->fails()) {
    return redirect()->back()->withErrors($validator)->withInput();
}




But I do recommend You to create ContactFormRequest class that extends Request class and put it in store argument (screenshot: http://joxi.ru/eAO55BF4glwRmo):

<?php namespace App\Http\Requests;   

class ContactFormRequest extends Request {

    public function rules() {
        return [
            'ContactFirst' => 'required|max:25',
            'ContactLast' => 'required|max:25',
            'ContactRole' => 'required|max:25',
            'ContactEmail' => 'email|max:255',
            'ContactPhone' => 'max:255',
        ];
    }
}

and then in Your controllers store method do it like:

public function store(ContactFormRequest $request) {
    // here write code as if validation is valid
}

if after this manipulation You still cannot get errors so put:

<?php var_dump(get_defined_vars()) ?>

before {{var_dump($errors)}}

3
votes

This seems to be related to an error running multiple versions of a similar site on Homestead. Destroying the box and rebuilding it fixed the issue.

0
votes

I am using the same version. Here is what I use to show errors.

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