4

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
2
  • 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). Commented Jun 23, 2016 at 0:05
  • @TiagoRL-- yes, the web middleware is not being applied in the routes.php Commented Jun 23, 2016 at 16:08

3 Answers 3

3

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)}}

Sign up to request clarification or add additional context in comments.

5 Comments

@num8er-- I think that's the old way. I'm using the newer syntax as documented here: laravel.com/docs/master/…
@user101289 old way is more classic and predictable. But if we insist on new way, so I'll write custom Request class and it will be super flexible to use, because validation rules does not garbage main logic of controller action, it keeps action code clean.
@num8er-- I agree with you-- I am just trying to use the documented format and am not sure why it fails to work as it should
also are You sure that email param is not required thing? maybe it's main issue that You cannot get errors?
@num8er-- required by what? It's not required in my validation block. What else could be requiring it?
3

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.