Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony/Twig: How to get error Message from hidden fields

Tags:

php

twig

symfony

I have defined 3 hidden fields in my formtype:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('type', 'hidden', array(

            ))
            ->add('number', 'hidden', array(

            ))
            ->add('token', 'hidden', array(

            ))
        ;
    }

When I send my form, I get a notValid Error from my Controller, thats totally correct. But when I want to get the errors in my twig-template, no errors were set.

{{ dump(myForm.card.type.vars.errors|length) }} //<--- IS ALWAYS 0

But when I change the formtype fields to "text" instead of "hidden" I get the correct length of 3.

Is it different to get the erros for hidden-fields?

THANKS FOR ANY HELP!!

like image 483
Zwen2012 Avatar asked May 12 '15 13:05

Zwen2012


1 Answers

I also meet this problem today, my solution is :

Set error_bubbling as false

$form->add('shippingAddress', 'hidden', [
    'label' => 'acme.form.checkout.addressing.shipping_address',
    'data' => $addressId,
    'error_bubbling'=>false
]);

Display the error using this way

{{ form_label(form.shippingAddress) }}
{{ form_errors(form.shippingAddress) }}
{{ form_widget(form.shippingAddress) }}
like image 122
Vidy Videni Avatar answered Sep 28 '22 15:09

Vidy Videni