4

I have a form request class to validate my data and I am using the messages() method to return custom validation error messages like so:

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.'
    ];
}

So as you can see I want to output a hyperlink in the error message to help the user. When the error message is output though all of the html tags have been converted to entities so that what is actually output is:

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.

How can I output HTML in my error message?

geoffs3310
  • 5,599
  • 11
  • 51
  • 104
  • Possible duplicate of [Laravel 5: Display HTML with Blade](http://stackoverflow.com/questions/29253979/laravel-5-display-html-with-blade) – Gerard Reches Nov 18 '16 at 09:58

2 Answers2

7

Turns out it was the way I was outputting the errors that was causing the HTML entities issue. I was doing the following:

@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

I had to change <li>{{ $error }}</li> to <li>{!! $error !!}</li>

geoffs3310
  • 5,599
  • 11
  • 51
  • 104
  • Just be careful if any user inputted values are displayed in those error messages. Make sure to escape those with `e($value)` – andrewtweber Oct 15 '15 at 16:39
0

Don't put a hyperlink in the message itself. Try using an @if @endif statement in your view if there are any error messages returned with the response

@if($errors and $errors->has('name.valid_name'))
he name is incorrect, please see the  <a href="">Help link</a>
@endif
Glad To Help
  • 5,299
  • 4
  • 38
  • 56
  • I can't really do that because I output the errors in my template rather than the view to prevent duplicating code. Otherwise I'd have to put the following code at the top of every single view which doesn't seem logical: `if (count($errors) > 0)
    Whoops! There were some problems with your input.

      foreach ($errors->all() as $error)
    • {{ $error }}
    • endforeach
    endif`
    – geoffs3310 Oct 15 '15 at 09:29
  • you can always make a partial view and use an `@include` statement or make a sub-layout which has that `@include` statement and use it in all views where you have forms. – Glad To Help Oct 15 '15 at 09:33
  • 1
    you can still output the html in your view if you really want to.. `{!! $message !!}` – Glad To Help Oct 15 '15 at 09:35
  • But even if I did that I'd have to check for every possible error that could be set for any form throughout the system and output it if it exists. Its much cleaner and more efficient to just loop through the errors that have been set. – geoffs3310 Oct 15 '15 at 09:35
  • This way you are polluting the messages with html . Again I don't know the context of your app - to me it does not seem like you have to check the existence of every possible error in order to display a naming convention help link. it can be just one check if there are errors at all or not. Anyway, you know better, it's your app :) – Glad To Help Oct 15 '15 at 09:41