How to Retrieve validation Error Messages in Laravel View/Blade file?

blade-validadtion-error

Laravel makes working with your error messages an extremely easy using a simple error collector class. The error collector has several simple functions for retrieving your messages. So, in our example, the user will be redirected to our controller’s method when validation fails, you can display the error messages in the view like this:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Determine if an attribute has an error message:

if ($validation->errors->has('title'))
{
    // The title attribute has errors…
}

Retrieve the first error message for an attribute:

{{$validation->errors->first('title') }}

Sometimes you may need to format the error message by wrapping it in HTML. No problem. Along with the :message place-holder, pass the format as the second parameter to the method.

Format an error message:

{{$validation->errors->first('title', '<p>:message</p>')}}

To understand the laravel request validation in details, please check this tutorial here: How to validate Request data in Laravel?

Please follow and like us:

Related Posts

Leave a Reply

Share