0
votes

I have a CollectionType of EditAnnouncementType typeform I have created. This CollectionType will be used to render a form to handle a user editing some text of an Announcement, where each announcement has its own Edit modal that opens (the modals have unique ID's)

$editForm = $this->createFormBuilder()
        ->add('editForms', CollectionType::class,
            [
                'entry_type' => EditAnnouncementType::class,
                'allow_add' => true,
                'prototype' => true,
                'by_reference' => false,
                'required' => false,
        ])
        ->add('edit', SubmitType::class,
            array
            (
                'label' => 'Save changes',
                'attr' => ['class' => 'btn btn-primary']
            ))
        ->setData($this->getDoctrine()->getRepository(Announcement::class)->findAll())
        ->getForm()
        ;

How do I prefill N number of forms based on N number of rows (aka N announcement entities.)

formtype code

class EditAnnouncementType extends AbstractType

{ /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('edit', SubmitType::class, array ( 'label' => 'Save changes', 'attr' => ['class' => 'btn btn-primary'] )) ->add('id', HiddenType::class, []) ; }

/**
 * Returns the name of this type.
 *
 * @return string
 */
public function getName()
{
    return 'edit_announcement';
}

}

1

1 Answers

1
votes

Try with this

$editForm = $this->createFormBuilder()
        ->add('editForms', CollectionType::class,
            [
                'entry_type' => EditAnnouncementType::class,
                'allow_add' => true,
                'prototype' => true,
                'by_reference' => false,
                'required' => false,
        ])
        ->add('edit', SubmitType::class,
            array
            (
                'label' => 'Save changes',
                'attr' => ['class' => 'btn btn-primary']
            ))
        ->setData(['editForms' => $this->getDoctrine()->getRepository(Announcement::class)->findAll()])
        ->getForm()
        ;