dsz7121 2016-08-11 02: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 05: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
                ))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部