Laravel 5.2 $ errors empty

2

I'm using Laravel 5.2 and I'm trying to create a validation for user registration.

In UserController.php I have:

public function postSignUp(Request $request){
    $this->validate($request, [
        'email' => 'required|email|unique:users',
        'first_name' => 'required|max:120',
        'password' => 'required|min:4'
    ]);
    ...
}

And in the View for SignUp I have:

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

But when you try to register a user with an existing email or even put no text in the inputs, the div .col-md-6 is created but does not return anything.

I gave a var_dump in $ errors and this appears:

object(Illuminate\Support\ViewErrorBag)[133]
  protected 'bags' => 
array (size=1)
  'default' => 
    object(Illuminate\Support\MessageBag)[134]
      protected 'messages' => 
        array (size=3)
          ...
      protected 'format' => string ':message' (length=8)
    
asked by anonymous 10.05.2016 / 20:15

1 answer

2

The problem is that you are calling $errors->all . This is calling a MessageBag property, which does not exist.

In fact, you should call the MessageBag::all() method. So:

@foreach( $errors->all() as $error)
  <li>{{ $error }}</li>
@endforeach

Understand that MessageBag is not a array , but an object. You can only use the count function in the $errors variable, because MessageBag implements the Countable interface.

If you want to understand more about how this class works, see:

link

    
11.05.2016 / 04:38