3

My validation form works fine and it looks like this:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ')) !!}
{{$errors->first('email')}}

If the email is not good, I get this error: The a email has already been taken.

The thing is that I wan't to put a css class on the input also like error in order to add a red background border to the input.

How can I do this?

1

7 Answers 7

8

You can use if condition for check errors:

 <div class="form-group {{ $errors->has('email') ? 'has-error' :'' }}">
   {!! Form::text('email',null,['class'=>'form-control','placeholder'=>'Email Address']) !!}
   {!! $errors->first('email','<span class="help-block">:message</span>') !!}
</div>
Sign up to request clarification or add additional context in comments.

Comments

5

This was the correct answer for me, without using additional div's :

{!! Form::email('email', null, $attributes = $errors->has('email') ? array('placeholder' => 'Email', 'class' => 'form-control has-error') : array('placeholder' => 'Email', 'class' => 'form-control')) !!} 
{{$errors->first('email')}}

3 Comments

The problem on this one is Input does not take .has-error class in Bootstrap. This needs to be applied from parent div. Otherwise, this was a good solution.
i only need to add a class for my input fields to have a red border and this solution worked for me, it is a very long approach though.
i deleted the $attributes = and it worked the same.
4

you can use the following code to check if email field has Any validation error

 <div class="form-group has-feedback @if($errors->has('email') has-error @endif">
        <input name="email" class="form-control"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
        @if ($errors->has('email')) <p class="help-block"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
            {{ $errors->first('email') }}</p>
        @endif
    </div>

1 Comment

useful answer for when you're trying to implement into a theme / styled form. Take note of the syntax error though in the first div class; if statement bracket ) is missing
1

You could just add HTML around it and style it however you want.

HTML:

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

CSS:

.error {
    border: 1px solid red;
}

Edit: Add the has-error class to the input itself like this:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ' . $errors->first('email', 'has-error'))) !!}

2 Comments

I don't need a new element created. I need a css class added to my email input.
There's a very simple solution to that. :) Check my edit.
0

Add custom error message

'custom' => array(
    'email' => array(
        'required' => 'We need to know your e-mail address!',
    ),
),

Custom Error Messages in Laravel

1 Comment

I dont need a custom error message. I need a css class added to my input.
0
<script> 
  if({{$errors->has('email')}}){
    $(input[type="email"]).css('border-color', 'red');
  }
</script>

Comments

-1

add class has-error to your form like this

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}

you can use if condition if there's an error, set the class in

@if($errors->has('email'))
    {!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}
@else
    {!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control')) !!}
@endif

or you can do like this

<input class="form-control {{ $errors->has('email') ? 'has-error' : '' }}" name="email" placeholder="Email">

1 Comment

That class should appear only if the form validation failed, not always as you sugested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.