Laravel 5.1如何在表单验证错误消息中输出HTML

时间:2015-10-15 09:17:59

标签: php laravel laravel-5 laravel-5.1

我有一个表单请求类来验证我的数据,我使用messages()方法返回自定义验证错误消息,如下所示:

public function messages()
{
    return [
        'name.valid_name' => 'The name is incorrect, please see the <a href="'.route('naming_conventions').'">Naming Conventions</a> page for instructions on naming.'
    ];
}

因此,您可以看到我想在错误消息中输出超链接以帮助用户。当输出错误消息时,虽然所有html标记都已转换为实体,因此实际输出的是:

The name is incorrect, please see the &lt;a href=&quot;http://local.website.com/naming-conventions&quot;&gt;Naming Conventions&lt;/a&gt; page for instructions on naming.

如何在错误消息中输出HTML?

2 个答案:

答案 0 :(得分:3)

原来这是我输出导致HTML实体问题的错误的方式。我正在做以下事情:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

我必须将<li>{{ $error }}</li>更改为<li>{!! $error !!}</li>

答案 1 :(得分:0)

不要在邮件本身中放置超链接。如果响应中返回任何错误消息,请尝试在视图中使用@if @endif语句

@if($errors and $errors->has('name.valid_name'))
he name is incorrect, please see the  <a href="">Help link</a>
@endif