如果缺少,将当前对象值添加到 ChoiceType 值
Add current object value to the ChoiceType values if missing
我目前正在将应用程序转换为 Symfony 3.3,我正在绞尽脑汁想知道如何才能实现以下目标。
我有一个 ChoiceType
字段,称为 Responsible
(String
值),它是从数据库视图中填充的。我希望在进入编辑模式时看到 Responsible
字段已经填充,这是在记录 Responsible
值是 Responsible
字段值的一部分时这样做的。
但是从那以后值已经改变了,所以当我开始编辑现有记录时,它会显示为 Please Select 当值不是已经填充的值。
我的目标是将该缺失值添加到 Responsible
字段值中,以便预先选择它,但我还没有找到方法。
我试图查看 ChoiceType
documentation 中是否有选项,但似乎我必须去 onPreSetData
事件才能这样做,但即使在那里,我只能找到如何动态添加字段,而不是现有字段的值。
有人知道如何这样做吗,"proper" 这样做的方法是什么?
谢谢。
编辑:感谢@matval 的回答,如果当前值在选项中,则只缺少一些东西可以找到,所以我们没有重复的值,例如 if (!array_key_exists($entity->getResponsible(), $choices))
.
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// Some preceding code...
// onPreSetData
->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSet'))
}
/**
* On pre set event.
*
* @param FormEvent $event
* @throws \Exception
*/
public function onPreSet(FormEvent $event)
{
// Get entity & form
$entity = $event->getData();
$form = $event->getForm();
// Fill choices with responsibles from the users table
$choices = $this->fillResponsibles();
// If the key does not exists in the choices, add it.
if (!array_key_exists($entity->getResponsible(), $choices)) {
$choices[$entity->getResponsible()] = $entity->getResponsible();
}
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
'placeholder' => '-- Please Select --',
'label' => 'Responsible',
'required' => false,
]);
}
表单事件是正确的方法。它们是制作动态表单的最佳方式。正如您在 symfony doc 中看到的那样,您应该在 PRE_SET_DATA
事件期间添加 Responsible
字段。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$entity = $event->getData();
$choices = ... // populate from db
$choices[] = $entity->getResponsible();
$form = $event->getForm();
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
]);
});
如果您想在表单类型中保留动态字段 Responsible
(可能会重复用于创建操作),您仍然可以使用相同的事件。您需要做的就是删除该字段并重新添加它。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$entity = $event->getData();
$choices = ... // populate from db
$choices[] = $entity->getResponsible();
$form = $event->getForm();
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
]);
});
我目前正在将应用程序转换为 Symfony 3.3,我正在绞尽脑汁想知道如何才能实现以下目标。
我有一个 ChoiceType
字段,称为 Responsible
(String
值),它是从数据库视图中填充的。我希望在进入编辑模式时看到 Responsible
字段已经填充,这是在记录 Responsible
值是 Responsible
字段值的一部分时这样做的。
但是从那以后值已经改变了,所以当我开始编辑现有记录时,它会显示为 Please Select 当值不是已经填充的值。
我的目标是将该缺失值添加到 Responsible
字段值中,以便预先选择它,但我还没有找到方法。
我试图查看 ChoiceType
documentation 中是否有选项,但似乎我必须去 onPreSetData
事件才能这样做,但即使在那里,我只能找到如何动态添加字段,而不是现有字段的值。
有人知道如何这样做吗,"proper" 这样做的方法是什么?
谢谢。
编辑:感谢@matval 的回答,如果当前值在选项中,则只缺少一些东西可以找到,所以我们没有重复的值,例如 if (!array_key_exists($entity->getResponsible(), $choices))
.
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// Some preceding code...
// onPreSetData
->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSet'))
}
/**
* On pre set event.
*
* @param FormEvent $event
* @throws \Exception
*/
public function onPreSet(FormEvent $event)
{
// Get entity & form
$entity = $event->getData();
$form = $event->getForm();
// Fill choices with responsibles from the users table
$choices = $this->fillResponsibles();
// If the key does not exists in the choices, add it.
if (!array_key_exists($entity->getResponsible(), $choices)) {
$choices[$entity->getResponsible()] = $entity->getResponsible();
}
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
'placeholder' => '-- Please Select --',
'label' => 'Responsible',
'required' => false,
]);
}
表单事件是正确的方法。它们是制作动态表单的最佳方式。正如您在 symfony doc 中看到的那样,您应该在 PRE_SET_DATA
事件期间添加 Responsible
字段。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$entity = $event->getData();
$choices = ... // populate from db
$choices[] = $entity->getResponsible();
$form = $event->getForm();
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
]);
});
如果您想在表单类型中保留动态字段 Responsible
(可能会重复用于创建操作),您仍然可以使用相同的事件。您需要做的就是删除该字段并重新添加它。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$entity = $event->getData();
$choices = ... // populate from db
$choices[] = $entity->getResponsible();
$form = $event->getForm();
$form->add('responsible', ChoiceType::class, [
'choices' => $choices,
]);
});