【问题标题】:How to add class to the error messages in Laravel automaticallly如何在 Laravel 中自动将类添加到错误消息中
【发布时间】:2015-02-23 12:21:10
【问题描述】:
在我使用的刀片中
{{ $errors->first('VehicleNumber') }} to throw the errors.
但我怎样才能将 div 类添加到它。
我试过了
{{ $errors->first('VehicleNumber', array('class' => 'error')) }}
但它会抛出数组到字符串的转换错误。
我不需要
<div class="error">
{{ $errors->first('VehicleNumber') }}
</div>
如果我这样做,类错误将始终出现。
但我只想在错误存在时出现该类
【问题讨论】:
标签:
php
laravel
laravel-4
laravel-validation
【解决方案1】:
您使用第二个函数参数来格式化消息:
{{ $errors->first('VehicleNumber', '<div class="error">:message</div>') }}
【解决方案2】:
因为这篇文章似乎有点过时,但在 Google 中的排名仍然很高,这对我有用,因为 The Shift Exchange 提供的解决方案并不完全正确。
为了解释刀片中的 html,您需要使用以下语法:
{!! $errors->first('VehicleNumber', '<div class="error">:message</div>') !!}
注意 {{ }} 和 {!! !!}。第二种语法解释 HTML,第一种没有。
希望对某人有所帮助。