Silex / Symfony表单 - form_errors(表单)在twig中不起作用

时间:2016-08-18 16:07:26

标签: symfony-forms silex

我认为这很简单。

我有一个Silex应用程序,带有Symfony Form(带Validator)。

这是我表格的一部分:

return $this->factory->createBuilder(FormType::class)
        ->add('holidayId', HiddenType::class, array())
        ->add('firstName', TextType::class, array(
            'label' => 'Il tuo nome'
        ))
        ->add('lastName', TextType::class, array(
            'label' => 'Il tuo cognome'
        ))
        ->add('email', EmailType::class, array(
            'label' => 'La tua email',
            'constraints' => array(
                new Assert\NotBlank(),
                new Assert\Email()
            ),
            'invalid_message' => 'Indirizzo email non valido',
        ))
        ->add('phone', TextType::class, array(
            'label' => 'Cellulare',
            'constraints' => array(
                new Assert\NotBlank(),
                new Assert\Regex("/[\d\-\ ]+/"),
                new Assert\Length(array('min' => 5, 'max'=>20))
            )
        ))
        ->add('city', ChoiceType::class, array(
            'placeholder' => 'Seleziona una città',
            'choices' => $cities,
        ))
        ->add('age', NumberType::class, array(
            'label' => 'Età',
            'constraints' => array(
                new Assert\NotBlank(),
                new Assert\Range(array('min'=>18, 'max'=>100))
            )
        ))
        ->add('gender', ChoiceType::class, array(
            'label' => 'Sesso',
            'choices' => array(
                'Uomo' => 0,
                'Donna' => 1
            ),
            'expanded' => true,
            'constraints' => array(
                new Assert\NotBlank(),
                new Assert\Choice(array(0, 1))
            )
        ))
        ->add('privacy', ChoiceType::class, array(
            'choices' => array(
                'Dichiaro di aver preso visione della privacy policy e autorizzo il trattamento dei dati personali per le finalità di cui ai punti a) b) c) della stessa.' => 1,
            ),
            'expanded' => true,
            'multiple' => true
        ))
        ->add('marketing', ChoiceType::class, array(
            'choices' => array(
                'Presto il consenso per l’utilizzo e la comunicazione dei miei dati a terzi da parte di P&R Eventi e Vacanze s.r.l. per finalità promozionali relative ai servizi turistici e per la ricezione di offerte commerciali.' => 1,
            ),
            'expanded' => true,
            'multiple' => true,
            'required' => false
        ))
        ->add('send', SubmitType::class, array(
            'label' => 'INVIA RICHIESTA',
            'attr' => array('class' => 'btn btn-corporate btn-lg btn-block'),
        ))
        ->getForm();

返回Form的实例。

在我的控制器上我有:

    $form = $requestType->getForm();
    $form->handleRequest($request);

    if($form->isValid()){
        $data = $form->getData();

        // .....

        return $app->redirect('/thank-you');
    }

验证工作正常,当表单有效时重定向我。

在我的树枝视图中,我渲染表格:

{{ form_start(form) }}
{{ form_errors(form) }}
<div class="form-group">
    {{ form_label(form.firstName) }}
    {{ form_widget(form.firstName, {'attr': {'class': 'form-control'}}) }}
</div>
<div class="form-group">
    {{ form_label(form.lastName) }}
    {{ form_widget(form.lastName, {'attr': {'class': 'form-control'}}) }}
</div>
<div class="form-group">
    {{ form_label(form.email) }}
    {{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
</div>
...

如果表单无效,form_errors方法不会显示任何内容。永远是BLANK ....

出了什么问题?

提前谢谢!

微米。

1 个答案:

答案 0 :(得分:0)

我认为默认情况下该表单不包含子级错误

为每个元素添加错误输出

<div class="form-group">
    {{ form_label(form.firstName) }}
    {{ form_errors(form.firstName) }}
    {{ form_widget(form.firstName, {'attr': {'class': 'form-control'}}) }}
</div>

或者为此表单添加特殊类型,并更改构建器以将所有错误传递给视图

namespace Form;

class Edit extends AbstractType
{
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        parent::buildView($view, $form, $options);

        $view->vars = array_replace($view->vars, array(
            'errors' => $form->getErrors(true)
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
            ->add('holidayId', HiddenType::class, array())
            ->add('firstName', TextType::class, array(
                'label' => 'Il tuo nome'
            ))
        ...
    }
}

....

$form = $app['form.factory']->create(\Form\Edit::class, [], []);