0

The problem I am having is I have multiple validation rules on my date of birth field.

        @if($errors->has('dob'))
            <span id="helpBlock" class="help-block error">
                @foreach($errors->get('dob') as $message) 
                    {{ $message }}
                @endforeach
            </span>
            <script>$('#dob').addClass('formError');</script>
        @endif

This will print out ALL of the errors.

I only want to print out the 'required' validation rule if it gets triggered, I am handleing the other validation in the front end.

1
  • 1
    This doesn't really answer your question, but you probably shouldn't depend on front end validation only. Commented Mar 31, 2017 at 15:53

2 Answers 2

2

I had this same issue a while back. You can only access the error message bag via its public methods, $errors->all(), $errors->get(), $errors->first() etc.The first two methods return numeric arrays of the messages. Therefore, there's no way to filter by the validation name. So the best thing is to know the order of the messages based on your validation rules. So if for example, 'required' is the second rule defined for that field, you can access it by using it's index. Like this.

@if($errors->has('dob'))
        <span id="helpBlock" class="help-block error">
                {{ $errors->get('dob')[1] }}
        </span>
        <script>$('#dob').addClass('formError');</script>
    @endif

Sorry for bad english. Hope you understand. Have a nice day.

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

Comments

2

Remove the foreach and do

{{ $errors->first('dob') }}

2 Comments

Can you be sure the 'required' rule will be first?
If required is the first rule required will be first.

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.