Laravel 5.2 $errors empty

Asked

Viewed 217 times

2

I am using Laravel 5.2 and am trying to create a validation for user record.

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 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 trying to register a user with existing email or even without putting any text in the inputs, the div .col-Md-6 is created but returns nothing.

I var_dump $errors and this comes up:

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)

1 answer

2


The problem is you’re calling $errors->all. This you are calling a property of Messagebag, which does not exist.

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

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

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

If you want to understand more about the functioning of this class, see:

https://laravel.com/api/5.2/Illuminate/Support/MessageBag.html

  • Really, Voce is correct ! It was a syntax error that went unnoticed. In case missing the parentheses after all. Thank you very much !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.