2013-08-02 71 views
0

确认字段我不能找到一个办法让Laravel突出显示正确的领域,当_confirmation字段不正确。Laravel - 高亮错误

使用引导布局,我收到一封电子邮件,并email_confirmation场在我的形式是这样的:

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

因此,如果用户将在一个无效的电子邮件地址,然后在“电子邮件”字段将有一个错误附加到它,正确的标签/字段将突出显示。

但是,如果用户已经在第一个字段中输入一个有效的/正确的电子邮件地址,但得到确认错误 - 返回的错误依然为“电子邮件”字段,而不是email_confirmed领域。

对我来说,看起来怪怪的时候,当错误实际上是与email_confirmation场电子邮件字段中突出显示。

据我可以从this stackoverflow question明白,我大概可以做这样的事情

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

这将工作,但问题是,我运行一个多语种的网站,这样的:消息得到返回将是许多可能性之一。

我想我可以写一个函数来比较:消息针对每种语言的消息的数组,但我想我会检查是否有去它更简单的方法。

干杯!

+0

laravel有一个内置的本地化类,你可以与表单验证使用http://laravel.com/docs/validation#localization – astroanu

回答

-2

或者你可以只打印所有错误消息某处

$messages = $validator->messages(); 
    echo '<ul>'; 
    foreach ($messages->all() as $message) 
     { 
      echo '<li>'.$message.'</li>'; 
     } 
    echo '</ul>'; 
+0

是的,我们已经决定在登记表上有错误,做一个通知,但我的设计师反对所有错误的完整列表,并希望内联错误。这不是一个biggy(其实我已经完全忘记了我已经问过这个问题!) – alexleonard

+0

确定:d 你仍然可以批准我的回答是公认的,如果ü想:d – aimiliano

+0

@aimiliano你没有回答这个问题。你正在回答提问者提出的问题。他们希望它与表单域内联,而不是列表。 – Jonathan

1

我这是怎么显示内嵌错误,如果标题字段为空:

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

而我的验证规则:

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

使用Laravel 4和Bootstrap 3.