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?

1

3 Answers 3

8

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Just be careful if any user inputted values are displayed in those error messages. Make sure to escape those with e($value)
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

5 Comments

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) <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
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.
you can still output the html in your view if you really want to.. {!! $message !!}
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.
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 :)
0

I don't know how long this class has existed, but with the current version you might want to try this:

use Illuminate\Support\HtmlString;

...

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

With HtmlString you tell Blade that the value is already HTML-safe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.