Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 invalid dynamic form with no error

I got a problem with a dynamic form on symfony2. I'm trying to generate some fields for a submitted form. In others words, the user enters some values, submits the form, and according to these values, my dynamics fields are added to this same form (which is, obviously, displayed a second time). To do that, I used this example from the cookbook : http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

So, here is my FormationType class

class FormationType extends AbstractType
{

private $em;
private $context;

public function __construct($em, $context) {
    $this->em = $em;
    $this->context = $context;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('date')
        ->add('type', 'choice', array(
            'mapped' => false,
            'choices' => Formationlist::getTypeTypes(false),
            'empty_value' => false,
        ))
        ->add('cost')
        ->add('travelCost')
        ->add('maximum')
        ->add('location')
        ->add('schedule')
    ;

    $formModifier = function(FormInterface $form, $type) {
        $formationList = $this->em->getRepository('CoreBundle:FormationList')->findBy(array("year" => 1, "type" => $type));

        $form->add('formationList', 'entity', array(
            'label'=> 'Titre formation', 
            'choices' => $formationList, 
            'class' => 'CoreBundle:FormationList', 
            'property' => 'title',)
                );

        };


    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use ($formModifier) {

            $data = $event->getForm();
            $type = $data->get('type')->getData();

            $formModifier($event->getForm(), $type);

        }
    );

    $builder->get('type')->addEventListener(
        FormEvents::POST_SUBMIT,
        function(FormEvent $event) use ($formModifier) {

            $type = $event->getForm()->getData();
            $formModifier($event->getForm()->getParent(), $type);
        }
    );
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'EXAMPLE\CoreBundle\Entity\Formation'
    ));
}

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

So, the two addEventListener work pretty well. The first time my form is displayed, the field in formModifier is not loaded, as expected. My controller class is the following one :

public function createAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $contextSrv = $this->get('example.service.context');
    $context = $contextSrv->getContext();

    $entity  = new Formation();
    $form = $this->createForm(new FormationType($em, $context), $entity);
    $form->bind($request);

    if ($form->isValid()) {

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('formation_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

Since one of my dynamic field can't be null, the first time the form is submitted, it can't be valid. So, the FormationType is loaded a second time. That means, if the field "type" was filled, my formModifier() function can load the dynamic field (formationList). Until there, everything works pretty well, and I got my new field.

But, after a second "submit" on the form...nothing happen. The page is just reloaded, and no errors are displayed.

I checked the form content with

var_dump($request->request->get('example_corebundle_formationtype'));

-> Every fields (including the dynamic one) are filled with valid values.

I also try this :

foreach($form->all() as $item) {
       echo $item->getName();
        var_dump($item->getErrors());
}

-> These lines don't show any error. But, the form is never valid.

var_dump($form->isValid());

-> It returns false. So the form is invalid.

Finally, if I remove the whole dynamic part, my form works. I don't understand what's wrong. There is no errors displayed by the form, and the csrf token seems right. Did I miss something ? Thanks for your help.

like image 947
TiPi Avatar asked Nov 01 '22 13:11

TiPi


1 Answers

I know this is a bit outdated but comes up quite high on Google.

The getErrors() metod returns only Form's global errors not error messages for the underlying fields, you need either getErrors(true) or more sophisticated method when using embeded forms in a form. Please see: https://knpuniversity.com/blog/symfony-debugging-form-errors for more information.

like image 98
Ethernal Avatar answered Nov 10 '22 03:11

Ethernal