Symfony2:具有空值的实体表单域 [英] Symfony2: Entity form field with empty value
问题描述
我有一个表单定义,它使用最远的伟大的字段类型 entity
。使用选项 query_builder
我选择我的值并显示。
i have a form definition which uses the so-far great field type entity
. With the option query_builder
I select my values and the are displayed.
令人伤心的部分是,我需要显示 null
默认值,如 all
(它是一个过滤器窗体)。我不喜欢选项
选项 entity
,因为我有数据库值和一个 FormType
不应该查询数据库。
The sad part is, I am required to display a null
default value, like all
(it's a filter form). I don't like the choices
option of entity
because I have database values and a FormType
shouldn't query the database.
到目前为止,我的方法是实现一个自定义字段类型,它扩展了 entity
并在列表的顶部添加一个空条目。字段类型被加载和使用,但不幸的是,虚拟值不显示。
My approach so far was to implement a custom field type which extends entity
and adds a null entry to the top of the list. The field type is loaded and used but unfortunately the dummy value is not displayed.
字段定义:
$builder->add('machine', 'first_null_entity', [
'label' => 'label.machine',
'class' => Machine::ident(),
'query_builder' => function (EntityRepository $repo)
{
return $repo->createQueryBuilder('m')
->where('m.mandator = :mandator')
->setParameter('mandator', $this->mandator)
->orderBy('m.name', 'ASC');
}
]);
表单类型定义:
class FirstNullEntityType extends AbstractType
{
/**
* @var unknown
*/
private $doctrine;
public function __construct(ContainerInterface $container)
{
$this->doctrine = $container->get('doctrine');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired('query_builder');
$resolver->setRequired('class');
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$class = $options['class'];
$repo = $this->doctrine->getRepository($class);
$builder = $options['query_builder']($repo);
$entities = $builder->getQuery()->execute();
// add dummy entry to start of array
if($entities) {
$dummy = new \stdClass();
$dummy->__toString = function() {
return '';
};
array_unshift($entities, $dummy);
}
$options['choices'] = $entities;
}
public function getName()
{
return 'first_null_entity';
}
public function getParent()
{
return 'entity';
}
}
推荐答案
替代方法是使用 ChoiceList
,其中包含从数据库生成的选项,然后在自定义选择表单类型中使用,这将允许
An alternative approach would be to use a ChoiceList
with choices that are generated from the database and then use that in a custom choice form type that will allow for an empty_value
.
选择列表
namespace Acme\YourBundle\Form\ChoiceList;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
class MachineChoiceList extends LazyChoiceList
{
protected $repository;
protected $mandator;
public function __construct(ObjectManager $manager, $class)
{
$this->repository = $manager->getRepository($class);
}
/**
* Set mandator
*
* @param $mandator
* @return $this
*/
public function setMandator($mandator)
{
$this->mandator = $mandator;
return $this;
}
/**
* Get machine choices from DB and convert to an array
*
* @return array
*/
private function getMachineChoices()
{
$criteria = array();
if (null !== $this->mandator) {
$criteria['mandator'] = $this->mandator;
}
$items = $this->repository->findBy($criteria, array('name', 'ASC'));
$choices = array();
foreach ($items as $item) {
$choices[** db value **] = ** select value **;
}
return $choices;
}
/**
* {@inheritdoc}
*/
protected function loadChoiceList()
{
return new SimpleChoiceList($this->getMachineChoices());
}
}
选择列表服务(YAML) kbd>
Choice List Service (YAML)
acme.form.choice_list.machine:
class: Acme\YourBundle\Form\ChoiceList\MachineChoiceList
arguments:
- @doctrine.orm.default_entity_manager
- %acme.model.machine.class%
自定义表单类型
namespace Acme\YourBundle\Form\Type;
use Acme\YourBundle\Form\ChoiceList\MachineChoiceList;
..
class FirstNullEntityType extends AbstractType
{
/**
* @var ChoiceListInterface
*/
private $choiceList;
public function __construct(MachineChoiceList $choiceList)
{
$this->choiceList = $choiceList;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$choiceList = $this->choiceList;
$resolver->setDefault('mandator', null);
$resolver->setDefault('choice_list', function(Options $options) use ($choiceList) {
if (null !== $options['mandator']) {
$choiceList->setMandator($options['mandator']);
}
return $choiceList;
});
}
public function getName()
{
return 'first_null_entity';
}
public function getParent()
{
return 'choice';
}
}
自定义表单类型服务(YAML)
acme.form.type.machine:
class: Acme\YourBundle\Form\Type\FirstNullEntityType
arguments:
- @acme.form.choice_list.machine
tags:
- { name: form.type, alias: first_null_entity }
您的表单
$builder
->add('machine', 'first_null_entity', [
'empty_value' => 'None Selected',
'label' => 'label.machine',
'required' => false,
])
;
这篇关于Symfony2:具有空值的实体表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!