如何限制已登录用户在表单中显示的信息?

时间:2012-06-01 14:43:39

标签: forms symfony

我有树主要实体,用户,工作组和项目。在它们之间有一个名为Collaboration的中间实体。实际上我可以提供一个表单来添加一个包含用户信息和工作组信息的项目。我想在表单中通过连接用户过滤工作组。

用户和工作组之间也存在关联,以了解谁是工作组的所有者。 (用户在工作组上有一对多,工作组在用户上有多对一)

# MyNiceBundle/Form/Type/CollaborationType.php
class CollaborationType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('project', new ProjectType());
        $builder->add('participant', 'entity', array(
            'class' => 'MyNiceBundle:User',
            'property' => 'email',
            'multiple' => false,
            'expanded' => true
        ));
        $builder->add('workgroup', 'entity', array(
            'class' => 'MyNiceBundle:Workgroup',
            'property' => 'name',
            'multiple' => false,
            'expanded' => true
        ));
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用事件来执行此操作。见这里:http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html

可能在FormEvents :: POST_SET_DATA事件上你会做这样的事情......

public function postSetData(DataEvent $event)
{
    $collaboration = $event->getData();
    $form = $event->getForm();
    $user = $collaboration->getParticipant();

    $form->add(
        $this->factory->createNamed('workgroup', 'entity', null, array(
                'class'         => 'MyNiceBundle:Workgroup',
                'required'      => false,
                'query_builder' => function(EntityRepository $er) use ($user) {
                    return $er->getWorkgroupsForUser($user);
                }
            )
        )
    );
}

答案 1 :(得分:0)

这个问题在这里回答了“passing data from controller to Type symfony2

# MyNiceBundle/Controller/ProjectController.php
// I passed as parameter argument the current logged user to my FormType
$form = $this->createForm(new CollaborationType($this->get('security.context')->getToken()->getUser()), new Collaboration);

# MyNiceBundle/Form/Type/CollaborationType.php
class CollaborationType extends AbstractType
{
    protected $user;

    public function __construct (User $user)
    {
        // Get the logged user
        $this->user = $user;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $user = $this->user;

        $builder->add('project', new ProjectType());
        $builder->add('participant', 'entity', array(
            'class' => 'MyNiceBundle:User',
            'property' => 'email',
            'multiple' => false,
            'expanded' => true
        ));
        // Here I sort workgroup by owner
        $builder->add('workgroup', 'entity', array(
            'class' => 'MyNiceBundle:Workgroup',
            'property' => 'name',
            'multiple' => false,
            'expanded' => true,
            'query_builder' => function(EntityRepository $er) use ($user) {
                return $er->createQueryBuilder('w')
                    ->where("w.owner = :user")
                    ->setParameter('user', $user);
            },
        ));
    }