Problem with embed on a collection of Forms Symfony Version 4.4.15:

If I submit the form, I get the error below

InvalidPropertyPathException

Could not parse property path "children[upcomingTours].children[[0]].children[address].children[street].data". Unexpected token "]" at position 36.

This is the hierarchy of my form

 - TourType (custom form)
        - UcomingTour (CollectionType)
             - AddressType (custom form)

HTLM

<div>
<label for="tour_upcomingTours_0_address_street">Adresse</label>
<input type="text" 
       id="tour_upcomingTours_0_address_street" 
       name="tour[upcomingTours][0][address][street]" 
       placeholder="Tapez un nom de rue">
</div>

When I submit the form I got the above error.

  1. Is this a symfony bug? If so, is it already fixed in version
    5.x?
  2. Has anyone ever encountered this error?
  3. Does anyone have a solution for this problem

TourType

class TourType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ......
            ->add('upcomingTours', CollectionType::class, [
                'label' => false,
                'entry_type' => UpcomingTourType::class,
                'allow_delete' => true,
                'allow_add' => true,
                'by_reference' => false,
            ])
        ;
    }
}

UpcomingTourType

class UpcomingTourType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
             .........
             ->add('address', AddressType::class, [
                    'label' => false,
             ])
    }

}

AddressType

class AddressType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ............
            ->add('street', null, [
                'required' => false,
                'label' => 'admin.address_line',
                'attr' => [ 'placeholder' => 'admin.placeholder_line'],
                'constraints' => [
                    new NotNull([
                        'message' => 'project.street_not_blank',
                    ])
                ]
            ])
           
        ;
    }
}

TourController

class TourController extends AbstractController
{
    /**
     * @Route("/tour/new/", name="dashboard_tour_new", methods={"GET","POST"},
     *          defaults={"_locale" = "%locale%"},
     *          requirements={"_locale" = "%app_locales%"})
     */
    public function new(Request $request): Response
    {
        $tour = new Tour();
        $upcomingTour = new UpcomingTour();
        $tour->getUpcomingTours()->add($upcomingTour);
        $form = $this->createForm(TourType::class, $tour);
        $form->handleRequest($request);
        ...

    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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