Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Adding Class If Statement

I'm attempting to add a class to a label element if an error exists but I'm coming up with a syntax error. Saying unexpected < so I"m confused on where my syntax error is.

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . @if ($errors->first('name')) $errors->first('name') @endif ]) !!}
0 likes
32 replies
mstnorris's avatar

This should do it:

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->first('name') ? $errors->first('name') : '']) !!}

Not sure if you meant to assign the class $errors->first('name') so here is a clearer way:

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->first('name') ? 'error' : '']) !!}
jrdavidson's avatar

Still shows the same error.

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . @if ($errors->first('name')) {!! $errors->first('name') !!} @endif ]) !!}
mstnorris's avatar

@xtremer360 what class do you want to be added to the element? Or does each field have their own class?

Bear in mind that if each field has their own class, it won't scale that well.

jrdavidson's avatar

The issue with this is that when the page loads initially with no errors it has the class state-error which shouldn't happen unless there is an error.

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->first('name') ? 'state-error' : '']) !!}
mstnorris's avatar

There shouldn't be anything in $errors->first('name') unless there is an error.

jrdavidson's avatar

Here's my full text input.

<!-- First Name Form Input -->
                        <div class="form-group">
                            {!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->first('name') ? 'state-error' : '']) !!}
                            {!! Form::text('name', null, ['class' => 'form-control']) !!}
                            @if ($errors->has('name'))
                                <em class="state-error" for="name">{!! $errors->first('name') !!}</em>
                            @endif
                        </div>
jrdavidson's avatar

I'm not duplicating there errors I'm attaching a class to the form label and displaying the actual error below the form input.

mstnorris's avatar

Does the error display below the input as expected?

jrdavidson's avatar

It does not show when there is no error and shows when there is an error.

mstnorris's avatar

Cool, which is what we want right? So just use the same check:

$errors->has('name') instead of $errors->first('name')

<!-- First Name Form Input -->
<div class="form-group">
    {!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->has('name') ? 'state-error' : '']) !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
    @if ($errors->has('name'))
        <em class="state-error" for="name">{!! $errors->first('name') !!}</em>
    @endif
</div>
jrdavidson's avatar

But that produces a error saying syntax error, unexpected '{', expecting ',' or ';'

{!! Form::label('name', 'Name:', [ 'class' => 'field ' . $errors->has('name') ? 'state-error' : '']) 
jrdavidson's avatar

I forgot to add it but when I did it still shows state-error in the label when it shouldn't and DOES NOT show field.

mstnorris's avatar

Try clearing your cache as you will always have access to an $errors array but it should be empty unless there are errors.

jrdavidson's avatar

Still getting the same thing for some reason. Why is it overriding the field class name.

martinbean's avatar

@xtremer360 The error class should be applied to your wrapping .form-group <div>. That way, all elements inside (labels, inputs, help blocks) will inherit the error styling:

<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
  {!! Form::label('name', 'Name', ['class' => 'control-label']) !!}
  <div class="form-controls">
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
  </div>
</div>
mstnorris's avatar

@martinbean that isn't the issue here. The issue is that the class isn't being added at all not where it is being added. Take a look over the previous posts, maybe a fresh pair of eyes can shed some light on the issue. I'm knackered now and my eyes are absolutely square.

bashy's avatar

Is it possible to do PHP statements inside the Form helpers?

Why would you want it on the label? You should be using the method of martinbean's

jrdavidson's avatar

@bashy Because its how the template was written is it applies the class on validation of errors. What do you mean function for @martinbean ?

bashy's avatar

CSS should be build to read the class on the surrounding div. What CSS are you using? Seems very bad if you have to add it onto the input itself...

jrdavidson's avatar

@bashy

This is the template before validation.

<div class="col-md-6">
    <label for="firstname" class="field prepend-icon">
        <input type="text" name="firstname" id="firstname" class="gui-input" placeholder="First name...">
        <label for="firstname" class="field-icon">
            <i class="fa fa-user"></i>
        </label>
    </label>
</div>
<!-- end section -->

This is the template after validation.

<div class="col-md-6">
    <label class="field prepend-icon state-error" for="firstname">
        <input id="firstname" class="gui-input" type="text" placeholder="First name..." name="firstname" aria-required="true" aria-invalid="true">
        <label class="field-icon" for="firstname">
    </label>
    <em class="state-error" for="firstname">Enter first name</em>
</div>
<!-- end section -->
bobbybouwmann's avatar

You can do this

<div class="col-md-6">
    <label class="field-icon" for="firstname">
        <input id="firstname" class="gui-input" type="text" placeholder="First name..." name="firstname" aria-required="true" aria-invalid="true" value="{{ old('firstname') }}>            
    </label>

    @if ($errors->has('firstname')
        <em class="state-error">{{ $errors->first('firstname') }}</em>
    @endif
</div><!-- end col-md-6 -->
jrdavidson's avatar

@bobbybouwmann Great idea however if you could please read the the older posts so you can understand the actual issue I'm having. Thank you for responding though.

bobbybouwmann's avatar

My code should just work fine! If it doesn't you have other problems...

Next

Please or to participate in this conversation.