Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Laravel 4 errors in TwigBridge?

I've installed TwigBridge for Laravel 4 and I am trying to adapt some templates I've already got from Blade to Twig.

I want display some validation errors at the top of a view.

I had the following in Blade (which worked OK):

@if (isset($errors))
    @foreach ($errors->all() as $error)
        <p>{{ $error }}</p>
    @endforeach
@endif

I've tried to convert it to Twig but nothing gets displayed.

{% if errors %}
    {% for error in errors %}
        <p>{{ error }}</p>
    {% endfor %}
{% endif %}

However, if I try:

{{ errors }} 

I do get some output:

{"name":["The name field is required."]}

What do I need to change in order to get it to work?

Any advice appreciated.

Thanks

like image 427
Dan Avatar asked Dec 12 '22 13:12

Dan


1 Answers

I worked it out after I looked at the code in Illuminate/Support/MessageBag:

{% if errors.any %}
    {% for error in errors.all %}
        <p>{{ error }}</p>
    {% endfor %}
{% endif %}
like image 60
Dan Avatar answered Dec 22 '22 21:12

Dan