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

t0berius's avatar

custom validator highlight form(error)

How can I use my custom validator to "mark" the wrong form element? I don't find any details about this. For example in case a user entered a wrong formatted "string" to my app the validator will fail and should redirect user back, show an error message AND the form should be highlighted, using bootstrap.

Example:

<div class="form-group has-error">
    <label class="control-label" for="inputError1">Input with error</label>
    <input type="text" class="form-control" id="inputError1">
</div>

What's the best way to do this IN the view and how can I tell the validator to "tell" the view which form element failed, so I can mark it?

0 likes
3 replies
tykus's avatar

This is what I have done in the past, but it's ugly and takes a while to grok when you come back to the view a few months later. Will be interested to see if there is a more elegant way

<div class="form-group @if($errors->has('name_of_field)) has-error @endif">

edit this is a little neater:

<div class="form-group {{ $errors->has('name_of_field') ? 'has-error' : ''}}">
1 like
t0berius's avatar

I thought there might be a "built in" functionality for a validator to do this...

tykus's avatar

I figure a Validator's job is to validate and return true, or false with errors - up to you how you want to handle them.

Please or to participate in this conversation.