Search code examples
phplaravelerror-handlinglaravel-validation

Laravel is displaying error message before form submit


in my last question i was asking for help with single error message displaying after incorrectly filled form instead of error for every incorrect input. They were wery helpfull and I received this piece of code:

          @if ($errors)
              <span class='help-block'>
                  <strong>{{ "There are errors" }}</strong>
              </span>
          @endif

and the second answer was

 @if (!empty($errors))
 <span class='help-block'>
     <strong>{{ "Some input field is not properly filled" }}</strong>
 </span>
 @endif

I thought that the above will be displayed only after form submiting and if Laravel will find any errors. The problem is that it's always displaying error message like in the screenshot under: enter image description here Does anyone knows the solution? Many Thanks.


Solution

  • $errors variable is always set. It's a message bag that might or might not contain messages.

    You could check like this:

     @if ($errors->count() > 0)
     <span class='help-block'>
         <strong>{{ "Some input field is not properly filled" }}</strong>
     </span>
     @endif
    

    @if (!$errors->isEmpty()) should also work.