0

I'm having a bit of a weird issue and i'm not quite sure why its happening or if i'm just missing something.

I have multiple validation rules on a input 'postcode' field, like so

'postcode' => [
                'required',
                'min:5',
                'max:8',
                'regex:/^[a-z]/i'
              ],

And i've also wrote custom messages for the rules like so

return [
     'postcode.min' => 'The postcode must be at least 5 characters',
     'postcode.regex' => 'Sorry, the postcode must start with a letter',
];

All of my errors are being looped and displayed like this

<div class="error-block {{ (count($errors) > 0) ? '' : 'hide' }}">
    <div class="col-12">
        <ul>
            @if(count($errors) > 0)
                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                @endforeach
            @endif
        </ul>
    </div>
</div>

But when both rules are hit for the postcode, for example with the input '123', then it pushes both errors out on the same line, but all other errors show correctly, like this

  • The postcode must be at least 5 characters,Sorry, the postcode must start with a letter.
  • The username field is required.
  • The address line 1 field is required.

Why is laravel pushing the min and regex rule messages out on the same line?

3
  • the other li's are different fields, right? var_dump($errors); exit; and see what you get. Commented Jul 19, 2018 at 10:18
  • To display errors, read: laravel.com/docs/5.6/… Commented Jul 19, 2018 at 10:18
  • Yes sorry, the form contains more fields than just the postcode. I just put some other error messages from the other fields as an example of how the postcode field is erroring. Commented Jul 19, 2018 at 10:18

2 Answers 2

1

As described in the docs, validation rules should be expressed as a string, with different rules for a single input separated by a pipe |. The set of inputs to be validated should be an array, but not the rules themselves.

In your case, for the postcode input:

'postcode' => 'required|min:5|max:8|regex:/^[a-z]/i',
Sign up to request clarification or add additional context in comments.

3 Comments

I originally done it this way, but doing it this way or the way in my OP doesn't fix the issue, it still happens either way,
@S_R Ah, now I see the note about using an array for regex validation in the docs, I wasn't aware of that. Maybe just as a test, remove the regex from the validation and see if it works?
It works fine without the regex in the validation, but I need the regex as part of it.
0

That means your $error is an array. if you want to list all error as new line, check if the error is also an array or is not an array.

 @if(count($errors) > 0)
        @foreach($errors->all() as $error)
            @if(is_string($error))
               <li>{{$error}}</li>
           @else
                @foreach( $error as $newBreakdown)
                     <li>{{$newBreakdown}}</li>
                @endforeach
           @endif
        @endforeach
    @endif

1 Comment

This doesn't seem to have fixed it, still having the same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.