1

I can't find a way to get Laravel to highlight the correct field when a _confirmation field is incorrect.

Using a Bootstrap layout I've got an email and email_confirmation field in my form like this:

<div class="control-group {{$errors->has('email') ? 'error' : ''}}">
    {{ Form::label('email', 'Email', array('class' => 'control-label'))}}
    <div class="controls">
        {{ Form::email('email', Input::old('email'));}}
        {{ $errors->first('email', Form::block_help(':message')) }}
    </div>
</div>
<div class="control-group {{$errors->has('email_confirmation') ? 'error' : ''}}">
    {{ Form::label('email_confirmation', 'Confirm Email', array('class' => 'control-label'))}}
    <div class="controls">
        {{ Form::email('email_confirmation', Input::old('email_confirmation'));}}
        {{ $errors->first('email_confirmation', Form::block_help(':message')) }}
    </div>
</div>

So if the user puts in an invalid email address then the 'email' field will have an error attached to it and the correct label/field will be highlighted.

But if the user has entered a valid/correct email address in the first field but gets the confirmation wrong - the error returned is still for the 'email' field, not the email_confirmed field.

To me it looks weird when the email field is highlighted when the error is actually with the email_confirmation field.

As far as I can understand from this stackoverflow question, I could probably do something like

{{$errors->first('email', ':message') == 'Please confirm your email address correctly.' ? 'error' : ''}}

This will work, but the problem is that I'm running a multi-lingual site so the :message that gets returned is going to be one of many possibilities.

I guess I could write a function to compare the :message against an array of messages for each language but I thought I'd check if there was an easier way to go about it.

Cheers!

Community
  • 1
  • 1
alexleonard
  • 1,314
  • 3
  • 21
  • 37
  • laravel has a built in Localization class which you can use with form validation http://laravel.com/docs/validation#localization – astroanu Jul 29 '14 at 13:04

2 Answers2

3

This is how I show an inline error if the title field is empty:

<div class="form-group @if ($errors->has('title')) has-error @endif">
    {{ Form::label('title', 'Title') }}
    {{ Form::text('title', null, array('class' => 'form-control')) }}
    @if ($errors->has('title')) <p class="help-block">{{ $errors->first('title') }}</p> @endif
</div>

And my validation rule:

public static $rules = array(
    'title' => 'required',
);

Using Laravel 4 and Bootstrap 3.

Thomas Jensen
  • 2,138
  • 2
  • 25
  • 48
-2

Or you could just print all the Error messages somewhere

 $messages = $validator->messages();
     echo '<ul>';
     foreach ($messages->all() as $message)
        {
            echo '<li>'.$message.'</li>';
        }
     echo '</ul>';
aimiliano
  • 1,105
  • 2
  • 12
  • 18
  • Yeah, we've decided to do a notice at the top of the registration form that there are errors, but my designer is against a full list of all errors and wants inline errors. It's not a biggy (in fact I'd completely forgotten I'd asked this question!) – alexleonard Dec 12 '13 at 11:32
  • Ok :D still you could approve my answer as accepted if u want:D – aimiliano Dec 12 '13 at 14:22
  • 1
    @aimiliano you're not answering the question. You're answering the exact opposite of what the questioner asked. They want it inline with the form fields, not as a list. – Jonathan May 31 '16 at 13:16