I have an EditAnnouncementType
form which is embedded in a CollectionType
form. The embedded form has two ChoiceType
forms, one which properly updates the mapped Announcement
entity. The other one however, will attempt to update all instances of the entity in my database, resulting in this error.
Type error: Argument 1 passed to AppBundle\Entity\Announcement::setType() must be of the type integer, null given, called in /Users/dperezpe/dev/grand-central/673/grand-central/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 528
EditAnnouncementType.php The type
form is the error one.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('edit', SubmitType::class,
array
(
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary']
))
->add('type', 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' => array(
'class' => 'sr-only'
),
])
->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
])
The CollectionType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('announcements', CollectionType::class,
[
'entry_type' => EditAnnouncementType::class,
'entry_options' => ['label' => false],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => AnnouncementManager::class
]);
}
I suspect it is related to the difference in the HTML name that was rendered to this, where the type inputs are missing an empty []
<input type="radio"
id="announcement_edit_collection_announcements_221_type_0"
name="announcement_edit_collection[announcements][221][type]"
required="required" value="1">
<input type="checkbox" id="announcement_edit_collection_announcements_221_audience_0"
name="announcement_edit_collection[announcements][221][audience][]
"value="students">