How to access Laravel 4 errors in TwigBridge?

I installed TwigBridge for Laravel 4, and I'm trying to adapt some of the templates that I already have from Blade to Twig.

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

In Blade (which worked fine) I had the following:

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

I tried converting it to Twig but nothing is displayed.

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

However, if I try:

 {{ errors }} 

I get some output:

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

What do I need to change to make it work?

Any advice is appreciated.

thanks

+4
source share
3 answers

I worked on this after looking at the code in the Illuminate / Support / MessageBag:

 {% if errors.any %} {% for error in errors.all %} <p>{{ error }}</p> {% endfor %} {% endif %} 
+10
source

I use the following to target the email field ...

 {{ errors.toArray['email'][0] }} 

Not sure if this is a great approach.

0
source

This should work:

 {{ errors.first('email') }} 
0
source

Source: https://habr.com/ru/post/1490452/


All Articles