Symfony 4 - 具有动态显示字段的相同类型的多种形式

时间:2018-02-19 10:58:23

标签: php forms symfony types

我想使用我的UserType表单类来注册用户并编辑用户配置文件。由于我希望管理员在设置角色时注册用户,我不希望用户修改这些选项。所以我想使用两种不同的形式,但使用相同的类型User

我可以在一个buildForm()课程中创建2个不同的FormType函数吗? 或者我需要创建另一种类型吗?

这是我的UserType课程(主要目的是注册用户):

<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', TextType::class)
            ->add('lastname', TextType::class)
            ->add('birthdate', BirthdayType::class, array(
                'placeholder' => '-'))
            ->add('email', EmailType::class)
            ->add('username', TextType::class)
            ->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class,
                'first_options'  => array('label' => 'password'),
                'second_options' => array('label' => 'repeat-password'),
            ))
            ->add('roles', ChoiceType::class, [
               'multiple' => true,
               'expanded' => true, // render check-boxes
               'choices' => [
                   'Administrateur' => 'ROLE_ADMIN',
                   'Direction' => 'ROLE_MANAGER',
                   'Comptabilite' => 'ROLE_ACCOUNTING',
                   'Commercial' => 'ROLE_MARKETING',
                   'Docteur' => 'ROLE_DOCTOR',
                   'Client' => 'ROLE_CLIENT',
               ],
           ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => User::class,
        ));
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用变量将角色动态添加到formType

3. Configuration - 3.1. Creating a ProcessEngine

然后传递像

这样的变量
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $this->is_admin = $options['is_admin'];
    // [...]
    if ($this->is_admin)
        $builder
           ->add('roles', ChoiceType::class, [
               'multiple' => true,
               'expanded' => true, 
               'choices' => [
                   'Administrateur' => 'ROLE_ADMIN',
                   'Direction' => 'ROLE_MANAGER',
                   'Comptabilite' => 'ROLE_ACCOUNTING',
                   'Commercial' => 'ROLE_MARKETING',
                   'Docteur' => 'ROLE_DOCTOR',
                   'Client' => 'ROLE_CLIENT',
               ],
           ])
        // [...]
    ;
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        // [...]
        'is_admin' => false,
    ]);
}

答案 1 :(得分:0)

您可以使用角色的控制器集array $options中的choices来轻松处理此问题,并将其添加到$option中,并在userType中执行以下操作:< / p>

->add('roles', ChoiceType::class, [
           'multiple' => true,
           'expanded' => true, // render check-boxes
           'choices' => $options['choices'],
       ])