I am trying to add some radio buttons to a mail merge section of a web app. If the user imports a csv of emails and the emails are already matched to an existing user they are given the option of making a new user account or using one of the emails already associated with the user. If they choose No from a dropdown to not add a new user I want a radio button to appear next to the available user accounts for them to choose. The problem is that I can't get the dropdown/radios to render for each user.
I've tried a parent form with a collectionType containing a child form type with the dropdown and radio buttons as choicetypes within the child form. Parent Form:
class EmailCampaignExistingUsersForm extends AbstractForm{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->existingUsers = [''];
if(isset($options['data'])) {
$this->existingUsers = $options['data'];
}
$builder
->add(
'newOrExistingUser',
CollectionType::class,
[
'entry_type' => EmailCampaignExistingUsersType::class,
'entry_options' => array(
'label' => false,
),
'data' => [$this->existingUsers],
'by_reference' => false,
'allow_add' => false,
'allow_delete' => false
]
);
}
And the child type:
class EmailCampaignExistingUsersType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('newUser',
ChoiceType::class,
[
'label' => 'Create new user?',
'expanded' => true,
'multiple' => false,
'choices' => [
'yes' => 1,
'no' => 0
],
]
)
->add('existing_user',
CollectionType::class,
[
'entry_type' => ChoiceType::class,
'entry_options' => array(
'label' => false
),
'by_reference' => false,
'allow_add' => false,
'allow_delete' => false
]);
}
Here is the twig:
<tbody>
{% for email, userArray in existingUsers %}
<tr>
<td colspan="3"><strong>{{ email }}</strong></td>
<td>{% for newUser in emailCampaignExistingUsersForm.newOrExistingUser %}
{{form_end(emailCampaignExistingUsersForm)}} {% endfor %}
</td>
</tr>
{% for users in userArray %}
{% for user in users %}
<tr>
<td>{{ user.getId() }}</td>
<td>{{ user.getName() }}</td>
<td>{{ user.getUserName() }}</td>
<td>Add this one to the list
{{form_end(emailCampaignExistingUsersForm)}}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
This is how it renders so far: it needs to have the Yes/No options for each bold email example, and a radio button for each user so that people can choose which user account to include on the mail merge.