【问题标题】:Symfony2 entity type check box set preferred choicesSymfony2 实体类型复选框设置首选选项
【发布时间】:2014-09-26 04:27:43
【问题描述】:

我在编辑表单中提交了以下表单。

->add('district', 'entity', array(
                    'class' => 'AdminBundle:Districts',
                               'query_builder' => function(EntityRepository $repository) {
                return $repository->createQueryBuilder('c')
                                    ->where('c.status =:status')
                                    ->setparameter('status','1');
            },
                    'property' => 'districtName',
                    'preferred_choices' => array($details->getDistrict()),
                    'multiple' => TRUE,
                    'expanded' => TRUE,
                    'required' => true,

                        )
                )

它的输出是复选框。我可以在这里查看更多地区。 在编辑模式下如何设置首选选项?

【问题讨论】:

  • 不确定我是否完全理解您的问题。您是否希望将更多地区定义为首选(而不是像您的示例中那样只定义当前选择的地区),以便它们位于列表顶部?
  • 是的,想查更多区
  • 好的,我还是不明白,对不起!我希望 Matthew A Thomas 的回答能给你你想要的(他描述了如何使某些选项成为“首选”,即将它们移动到列表的顶部,仅在编辑模式下(尽管 Symfony 文档建议 preferred_choices 不是真的像这里的复选框))。如果您想要其他东西,那么也许您可以编辑您的问题以包含您当前获得的示例以及您想要获得的示例?

标签: forms symfony entity


【解决方案1】:

好的,您需要对表单使用 EventListener。有关详细信息,请参阅文档。

这将允许您预先设置表单数据

例子()

/* 形式 */

 namespace Company\YourBundle\Form\Type;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Doctrine\ORM\EntityRepository;
 use Company\YourBundle\Form\EventListener\YourEventListener;

 class FormType extends AbstractType {

     public function buildForm(FormBuilderInterface $builder, array $options) {
         $builder->addEventSubscriber(new YourEventListener($builder->getFormFactory()));
     }

     public function getName() {
         return 'company_formtype';
     }

}

/* 事件监听器(您可能需要从表单中将更多数据传递给此类,因为我没有多少信息可以帮助您)*/

 namespace Company\YourBundle\Form\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ActivityListener implements EventSubscriberInterface {

private $form;

public function __construct($form) {
    $this->form = $form;
}

public static function getSubscribedEvents() {
    return array(
        FormEvents::PRE_SET_DATA => 'onPreSetData',
    );
}

public function onPreSetData(FormEvent $e) {
    $data = $e->getData();
    $form = $e->getForm();

    if ($form->has('district')) {
        $form->remove('district');
    }

    $form->add($this->form->createNamed('district', 'entity', null, array(
                        'class' => 'AdminBundle:Districts',
                           'query_builder' => function(EntityRepository $repository) {
            return $repository->createQueryBuilder('c')
                                ->where('c.status =:status')
                                ->setparameter('status','1')},
                                'property' => 'districtName',
                'preferred_choices' => $data['id'] ? /** in edit mode set the preferred **/ ? null,
                'multiple' => TRUE,
                'expanded' => TRUE,
                'required' => true,

                    ));
      }

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多