samedi 30 septembre 2017

Validate and add a subentity only if checkbox is checked

I have a user entity. The user may check a checkbox, and fill an additional entity of data. Thes data will be saved with doctrine

My form type looks like this:

...
$builder->add('name', null, array('error_bubbling' => true))
->add("hasSocieta", CheckboxType::class, array('mapped' => false, 'required'=> false))
->add("social_links", SocialType::class array('error_bubbling' => true, 'required' => false, 'validation_groups' => array('social'))

public function configureOptions(OptionsResolver $resolver)
{
 $resolver->setDefaults([
                'validation_groups' => function(FormInterface $form) {
                    if ($form->get('hasSocial')->getData() == false) {
                        return array('Default');
                    }
                    return array('Default', 'social');
                }
          ]);
}

I'll hide from the form this additional entity with javascript unless the user checkes the checkbox, but I'll have to show it the user has js deactivated.

When I submit the form, If I input one of the fields inside the Social entity, but not the others, a doctrine exception has raised (because it needs the other fields). How can I discard all the data associated to this entity, if the checkbox is checked?




Aucun commentaire:

Enregistrer un commentaire