AngularAngularAngular AngularAngularAngular - 1 month ago 10
PHP Question

How to add class to the error messages in Laravel automaticallly

In the Blade i use

{{ $errors->first('VehicleNumber') }} to throw the errors.


But How can i add the div class to it.

I tried

{{ $errors->first('VehicleNumber', array('class' => 'error')) }}


But It throws Array to String Conversion Error.

I don't need

<div class="error">
{{ $errors->first('VehicleNumber') }}
</div>


If i do so the class error will always appear.

But I want to appear the class only if the error exist

Answer

You use the 2nd function parameter to format the message:

{{ $errors->first('VehicleNumber', '<div class="error">:message</div>') }}
Comments