Sonata Form ChoiceFieldMaskType

I’m using the ChoiceFieldMaskType from Sonata. I want to display some fields when one field true. Here’s what I’ve done:

->add('hasArtist', ChoiceFieldMaskType::class, [
                'label' => 'Mettre un artiste sur l'article?',
                'choices' => [
                    'Oui' => 'true',
                    'Non' => 'false',
                ],
                'map' => [
                    'true' => ['artistName', 'artistText','artistLink','artistImage'],
                ],
                'required' => true
            ])
            ->add('artistName', TextType::class, ['required' => false, 'label' => 'Nom de l'artiste'])
            ->add('artistText', TextType::class, ['required' => false, 'label' => 'Texte pour l'artiste'])
            ->add('artistLink', TextType::class, ['required' => false, 'label' => 'Lien vers l'artiste'])
            ->add('artistImage', ElFinderType::class, array(
                                   'label' => 'Image de l'artiste',
                                   'instance' => 'form',
                                   'enable' => true,
                                   'required' => false,
                                   'attr' => array('class' => 'form-control')
                               )
            )

It works well, when hasArtist is set to yes, the other fields are displayed. The error is when I try to add the form. I have other fields, other than hasArtist, that are required. And if you don’t fill up artistName, artistText, etc fields it takes values from the other fields. This is my code for the form:

->with('Contenu de l'article')
            ->add('published', CheckboxType::class, ['required' => false, 'label' => 'Publier'])
            ->add('title', TextType::class, ['required' => true, 'label' => 'Titre'])
            ->add('textArticle', TextareaType::class, ['required' => true, 'label' => 'Contenu de l'article'])
            ->add('textLink', TextType::class, ['required' => true, 'label' => 'Texte pour le lien'])
            ->add('articleLink', TextType::class, ['required' => true, 'label' => 'Lien vers l'article'])
            ->add('marketingEtiquette', TextType::class, ['required' => false, 'label' => 'Etiquette Marketing'])
            ->add('mediaArticle', ElFinderType::class, array(
                             'label' => 'Photo de l'article',
                             'instance' => 'form',
                             'enable' => true,
                             'required' => true,
                             'attr' => array('class' => 'form-control')
                         )
            )
            ->add('mediaBackground', ElFinderType::class, array(
                             'label' => 'Photo dans le fond de l'article',
                             'instance' => 'form',
                             'enable' => true,
                             'required' => false,
                             'attr' => array('class' => 'form-control')
                         )
            )
            ->add('position',ChoiceType::class, array(
                'label' => 'Position dans la page',
                'choices' => array(
                    'Bloc haut de page' => 'top',
                    'Bloc bas de page' => 'bottom'
                )
            ))
            ->add('positionText',ChoiceType::class, array(
                'label' => 'Position du texte par rapport au média',
                'choices' => array(
                    'Texte à droite' => 'right',
                    'Texte à gauche' => 'left'
                )
            ))
            ->add('hasArtist', ChoiceFieldMaskType::class, [
                'label' => 'Mettre un artiste sur l'article?',
                'choices' => [
                    'Oui' => 'true',
                    'Non' => 'false',
                ],
                'map' => [
                    'true' => ['artistName', 'artistText','artistLink','artistImage'],
                ],
                'required' => true
            ])
            ->add('artistName', TextType::class, ['required' => false, 'label' => 'Nom de l'artiste'])
            ->add('artistText', TextType::class, ['required' => false, 'label' => 'Texte pour l'artiste'])
            ->add('artistLink', TextType::class, ['required' => false, 'label' => 'Lien vers l'artiste'])
            ->add('artistImage', ElFinderType::class, array(
                                   'label' => 'Image de l'artiste',
                                   'instance' => 'form',
                                   'enable' => true,
                                   'required' => false,
                                   'attr' => array('class' => 'form-control')
                               )
            )
            ->end();

And then I get an error

An exception occurred while executing 'INSERT INTO home_article (home_article_title, home_article_text, home_article_text_link, home_article_link, home_article_marketing_etiquette, home_article_artist_name, home_article_artist_text, home_article_artist_link, home_article_artist_image, home_article_media_article, home_article_media_background, home_article_published, home_article_has_artist, home_article_position, home_article_position_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [" Underground", "blabla.", "LIRE", "https:/underground", null, "/uploads/Z--Banner_1.webp", null, null, null, null, null, 1, 1, "top", "right"]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'home_article_media_article' cannot be null

Because I’ve added a constraint not null on home_article_media_article, which is ‘mediaArticle’ on the form code.
How can I fix that? How can I put by default null but if someone write something on the field then it takes this value?
I don’t know if it’s clear, I don’t really know how to explain this.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *