Besbes Riadh
Besbes Riadh

Reputation: 851

Add form error to a repeated FormType in Controller

I'm trying to add an error to a mail field of type repeated:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
         ->add('lastname', 'text', array(
                'required' => true,
                'trim' => true,
                'max_length' => 255,
                'attr' => array('placeholder' => 'lastname',
                )
            ))
         ->add('mail', 'repeated', array(
                'type' => 'email',
                'label' => 'email',
                'invalid_message' => 'Les Emails doivent correspondre',
                'options' => array('required' => true),
                'first_options' => array('label' => 'email',
                    'attr' => array('placeholder' => 'ph_mail_first',
                    )),
                'second_options' => array('label' => 'emailvalidation',
                    'attr' => array('placeholder' => 'ph_mail_second',
                        'requiered' => true
                    )),
                'required' => true,
                'trim' => true
            ))
}

public function getName()
{
    return 'AddUser';
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'csrf_protection' => true,
    ));
}

And this what I'm doing in my controller:

$sMessage = $this->container->get('translator')->trans('error');
$oForm->get('mail')->addError(new \Symfony\Component\Form\FormError($sMessage));

This method is working with the other fields except for this one, the error message does not appear. I also tried with

get('mail.first') and get('mail.first_options')

This works fine:

$sMessage = $this->container->get('translator')->trans('error');
$oForm->get('lastname')->addError(new \Symfony\Component\Form\FormError($sMessage));

Upvotes: 0

Views: 665

Answers (2)

Ignas Janickas
Ignas Janickas

Reputation: 13

$oForm->get('mail')->get('first')

Upvotes: 0

Besbes Riadh
Besbes Riadh

Reputation: 851

I managed to find a workaround. I honestlty don't know if this is the right solution but it can help someone stuck with this problem.

$oForm['mail']['first']->addError(new \Symfony\Component\Form\FormError($sMessage));

Upvotes: 1

Related Questions