У меня есть форма CollectionType
, которая содержит коллекцию из множества EditAnnouncementType
, которые сопоставляются с классом данных Announcement.php
. I have one
EditAnnouncementType for each existing
Объявление . The user can open up a window to edit a single announcement, which displays the
EditAnnouncementType`.
Моя проблема заключается в том, что при изменении одного объявления после выполнения кода $editForm->handleRequest($request);
текст других объявлений заменяется старым текстом отредактированного объявления. Однако ни у одной другой отредактированной черты нет этой проблемы, только текст.
Тип коллекции
class AnnouncementEditCollectionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('announcements', CollectionType::class,
[
'entry_type' => EditAnnouncementType::class,
'entry_options' => ['label' => false],
'label_attr' =>
[
'class' => 'sr-only'
]
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => AnnouncementManager::class
]);
}
}
EditAnnouncementType
class EditAnnouncementType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('save', SubmitType::class,
[
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary submit-edit announcementEditButton']
])
->add('content', TextareaType::class,
[
'attr' =>
[
'class' => 'form-control announcement-text',
'rows' => '5',
'name' => 'content',
'maxlength' => '2000'
],
'required' => true
])
->add('category', ChoiceType::class,
[
'choices' =>
[
'info_type' => 1,
'star_type' => 2,
'alert_type' => 3,
'lightbulb_type' => 4,
'event_type' => 5,
'statement_type' => 6,
'cat_type' => 7,
'hands_type' => 8
],
'expanded' => true,
'multiple' => false,
'required' => true,
'label_attr' =>
[
'class' => 'sr-only'
]
])
// can probably ignore this part
->add('id', HiddenType::class)
->add('deleted', HiddenType::class, ['data' => false])
->add('displayed', HiddenType::class)
->add('employees', HiddenType::class)
->add('students', HiddenType::class)
->add('audience', ChoiceType::class,
[
'choices' =>
[
'Students: anybody with a student level field populated' => 'students',
'Employees: anybody with an employee ID number' => 'employees'
],
'expanded' => true,
'required' => true,
'multiple' => true
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions (OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Announcement::class
]);
}
}
контроллер
//set the announcements from the DB into the AnnouncementManager to populate the editForm
$result = $this->announcementRepository->findByNotDeletedIdAsKey();
$this->announcementManager->setAnnouncements($result);
$editForm = $this->createForm(AnnouncementEditCollectionType::class, $this->announcementManager);
$editForm->handleRequest($request);
// Check each embedded edit form to see if the submit button for that form was clicked. If clicked,
// call the EditAnnouncementAction Method
foreach ($editForm->get('announcements') as $editAnnouncementForm) {
if ($editAnnouncementForm->get('save')->isClicked() and $editAnnouncementForm->isValid()) {
$announcementToEdit = $editAnnouncementForm->getData();
return $this->editAnnouncementAction($announcementToEdit);
//$this->entityManager->persist($announcementToEdit);
//$this->entityManager->flush();
}
}