paulalexandru
paulalexandru

Reputation: 9530

Laravel 5 Validation - error class on form elements

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?

Upvotes: 3

Views: 19930

Answers (7)

user2499205
user2499205

Reputation:

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>

Upvotes: 4

paulalexandru
paulalexandru

Reputation: 9530

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')}}

Upvotes: 5

Janko
Janko

Reputation: 202

<script> 
  if({{$errors->has('email')}}){
    $(input[type="email"]).css('border-color', 'red');
  }
</script>

Upvotes: 0

Thomas Kim
Thomas Kim

Reputation: 15941

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'))) !!}

Upvotes: 1

Madushan Perera
Madushan Perera

Reputation: 2598

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>

Upvotes: 8

yudijohn
yudijohn

Reputation: 1278

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">

Upvotes: -1

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

Add custom error message

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

Custom Error Messages in Laravel

Upvotes: 0

Related Questions