Symfony Form CollectionType,获取 entry_options 中活动条目的值
Symfony Form CollectionType, get the value of the active entry in entry_options
在 Symfony 中,我在 AbstractType
扩展 Class 中有一个函数 buildForm()
,我使用 CollectionType
,我想要获取entry_options
数组中的活动条目:
这是我的代码:
if($options['data']->getDescription()->isEmpty()){
$options['data']->getDescription()->set('fr',null);
$options['data']->getDescription()->set('en',null);
}
$builder->add('description',CollectionType::class,array(
'label'=>'MyBundle.messages.description',
'entry_type'=>TextType::class,
'entry_options'=>array(
'label'=>'JCABundle.messages.language_name.'.[active entry value]
)
));
我的愿望是用 'fr' 或 'en' 代替 [active entry value],知道 'description'(我在表单中使用的实体的属性)是一个包含 'fr 和 'en'.
的 ArrayCollection
所以它可以正确显示:'Français'、'Anglais'或'French'、'English'而不是'fr'、'en'作为标签.
有什么想法吗?
这里是根据发布的评论的解决方案,建议我在自定义 ***Type 上使用 EventListener:
我创建了一个新类型(注意:TexareaType 可以与 TextType 切换):
<?php
namespace MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class MultilangTextareaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
FormEvents::POST_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$mlta = $form->getViewData();
foreach($mlta as $mltaK => $mltaV){
$formOptions = array(
'label' => 'MyBundle.messages.language_name.'.$mltaK,
'constraints'=>array()
);
$form->add($mltaK, TextareaType::class, $formOptions);
}
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array());
}
public function getParent()
{
return CollectionType::class;
}
public function getBlockPrefix(){
return 'multilang_textarea';
}
}
?>
然后我在 buildForm()
中的初始代码变为:
if($options['data']->getDescription()->isEmpty()){
$options['data']->getDescription()->set('fr',null);
$options['data']->getDescription()->set('en',null);
}
$builder->add('description',MultilangTextareaType::class,array(
'label'=>'MyBundle.messages.description'
);
在 Symfony 中,我在 AbstractType
扩展 Class 中有一个函数 buildForm()
,我使用 CollectionType
,我想要获取entry_options
数组中的活动条目:
这是我的代码:
if($options['data']->getDescription()->isEmpty()){
$options['data']->getDescription()->set('fr',null);
$options['data']->getDescription()->set('en',null);
}
$builder->add('description',CollectionType::class,array(
'label'=>'MyBundle.messages.description',
'entry_type'=>TextType::class,
'entry_options'=>array(
'label'=>'JCABundle.messages.language_name.'.[active entry value]
)
));
我的愿望是用 'fr' 或 'en' 代替 [active entry value],知道 'description'(我在表单中使用的实体的属性)是一个包含 'fr 和 'en'.
的 ArrayCollection所以它可以正确显示:'Français'、'Anglais'或'French'、'English'而不是'fr'、'en'作为标签.
有什么想法吗?
这里是根据发布的评论的解决方案,建议我在自定义 ***Type 上使用 EventListener:
我创建了一个新类型(注意:TexareaType 可以与 TextType 切换):
<?php
namespace MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class MultilangTextareaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
FormEvents::POST_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$mlta = $form->getViewData();
foreach($mlta as $mltaK => $mltaV){
$formOptions = array(
'label' => 'MyBundle.messages.language_name.'.$mltaK,
'constraints'=>array()
);
$form->add($mltaK, TextareaType::class, $formOptions);
}
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array());
}
public function getParent()
{
return CollectionType::class;
}
public function getBlockPrefix(){
return 'multilang_textarea';
}
}
?>
然后我在 buildForm()
中的初始代码变为:
if($options['data']->getDescription()->isEmpty()){
$options['data']->getDescription()->set('fr',null);
$options['data']->getDescription()->set('en',null);
}
$builder->add('description',MultilangTextareaType::class,array(
'label'=>'MyBundle.messages.description'
);