dsz7121 2016-08-11 10:24
浏览 56
已采纳

Symfony 2:通过从ArrayCollection中加载manualy属性数据来预先选择表单中的多个值

I need to get preselected some values of an entity attribute that I get in the PRE_SET_DATA event, not from data base.

I have a Form working, all datas from my Entity AccessGroup is loaded but my problem is to get selected the ArrayCollection attribute named accessGroups from entity User which is not stored in database.

To make it clear, attribute accessGroups is loaded by User's roles.

Here is the FormType Class

namespace Pkg\ExtranetBundle\Form;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

use Doctrine\ORM\EntityManager;


class RoleType extends AbstractType
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->em = $options['em'];
        $builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
    }

    /**
     * Listener before normalizing data form
     *
     * @param FormEvent $event
     */
    public function onPreSetData(FormEvent $event)
    {
        $user = $event->getData();
        $accessGroups = $this->em->getRepository('PkgExtranetBundle:AccessGroup')->getSelected($user->getRoles());
        $user->setAccessGroups(new ArrayCollection($accessGroups));
        $event->setData($user);
        $form = $event->getForm();
        $form->add('accessGroups', EntityType::class, array(
                'class'         => 'PkgExtranetBundle:AccessGroup',
                'choice_label'  => 'name',
                'choice_value'  => 'role',
                'multiple'      => true,
                'expanded'      => false
            ))
            ->add('save', SubmitType::class, array('label' => 'registration.submit', 'translation_domain' => 'FOSUserBundle'));
    }


    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Pkg\ExtranetBundle\Entity\User',
            'em' => null
        ));
    }
}
  • 写回答

1条回答 默认 最新

  • dongying195959 2016-08-11 13:40
    关注

    Okey, the class I made has its own problem with the choice_value parameter :

    $form->add('accessGroups', EntityType::class, array(
                    'class'         => 'PkgExtranetBundle:AccessGroup',
                    'choice_label'  => 'name',
                    'choice_value'  => 'role', // If choice_value is not the entity index, then preselection will not be applied as the index could not be retrieved.
                    'multiple'      => true,
                    'expanded'      => false
                ))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助