【问题标题】:Symfony Form Select2 Allow User to Enter New ValuesSymfony Form Select2 允许用户输入新值
【发布时间】:2016-04-01 03:02:12
【问题描述】:

我有一个 Symfony 表单,其中有一个省字段,将其转换为 Select2。我想允许用户也允许新值。

 ->add('province', 'entity', array(
                'class' => 'PNC\GeneralBundle\Entity\State',
                'property' => 'name',
                'empty_value'=>'-- Select Province --',
                'label' => ucfirst('State / Province / Region'),
                'required'  => false,
                'attr' => array('class' => 'form-control'),
                'label_attr' => array('class' => 'control-label'),
                'mapped' => false,
            ))

树枝

$('#user_profile_type_province').select2({
    tags: "true",
});

控制器

if ($request->getMethod() == 'POST') {
 $UserProfileForm->handleRequest($request);
 $province = $UserProfileForm["province"]->getData();
 $tag = $em->getRepository('PNCGeneralBundle:State')->findOneByName($province);
 if(!$tag){
   $newState = new State();
   $newState->setName($province);
   $newState->setCountry($UserProfileForm["country"]->getData());
   $em->persist($newState);
   $em->flush();
   }
}

但是当我在省 select2 中进行新输入时,我得到了 provincenull

【问题讨论】:

标签: php jquery-select2 symfony-forms symfony


【解决方案1】:

它按预期工作:) 你没有实体,所以有空值。要使其工作,您必须创建一个 DataTransformer (http://symfony.com/doc/current/cookbook/form/data_transformers.html),如果状态不存在,您将在那里获取状态,您将创建一个新的实体对象。确保级联正确,以便新状态将与用户配置文件一起保存。

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 2014-10-26
    • 2015-02-24
    • 2023-04-02
    • 2013-01-17
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多