dongxuan1660 2018-09-13 16:14 采纳率: 100%
浏览 11

CollectionType中嵌入的ChoiceType仅渲染一次

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 %}                            

enter image description here

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化