I'm using Laravel Collective for my forms, and whenever the validation fails for a field, I need to add a class. The default field looks like this:
{{ Form::text('name', null, ['class' => 'form-control']) }}
Whenever validation fails, I need to add border-danger
to the class:
{{ Form::text('name', null, ['class' => 'form-control border-danger']) }}
But I cannot simply do @if ($errors->has('name'))
inside that {{ Form }}
field.
Is there any simple way to accomplish this? One thing I don't want to do is somthing like this:
@if ($errors->has('name'))
{{ Form::text('name', null, ['class' => 'form-control border-danger']) }}
@else
{{ Form::text('name', null, ['class' => 'form-control']) }}
@endif
You can use ternary operator like below
{{ Form::text('name', null, ['class' => 'form-control '.($errors->has('name') ? 'border-danger':'')]) }}
Use brackets to execute ternary operator first .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With