How do I display validation error messages?

There are multiple ways of displaying your validation errors. From the backend perspective, the request always returns errors to the view if validation had not passed.

 

Alert partial view

Aero has an error partial which automatically displays all the validation errors, provided that it is included in the view we want validation errors to display. To achieve this, simply paste this code under any @extends blade directive:

@include('admin::partials.alerts')

 

Admin

In the admin, we’d use Blade to display all validation errors at once like so:

@if($errors->isNotEmpty())
    <ul class="msg msg-error">
        @foreach($errors->all() as $error)
            <li class="font-bold">{{ $error }}</li>
        @endforeach
    </ul>
@endif

On the other hand, if we wish to display an error only for a specifed field - in this case firstname, we’d use:

@error('firstname')
    <div class="error">{{ $message }}</div>
@enderror

 

Store

In the frontend, we’re using Twig to display errors from a request. If we wish to display all validation errors, we’d use something along the lines of:

{% if errors %}
    <ul>
        {% for error in errors %}
               <li>{{ error }}</li>
        {% endfor %}
    <ul>
{% endif %}

If we wish to display an error for a particular field, in this case email:

{% if errors.has('email') %}
        {{ errors.first('email') }}
{% endif %}

Articles in this section

Was this article helpful?
0 out of 0 found this helpful