0

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));
1
  • Can you show us your entire form type please? Commented Nov 25, 2015 at 15:31

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.