gpt4 book ai didi

symfony-forms - 在 FormEvent 中获取 FormField 传递的选项

转载 作者:行者123 更新时间:2023-12-01 04:54:44 27 4
gpt4 key购买 nike

在 Symfony 项目中,我有一个表单 EventSubscriber 作用于多个表单。

它旨在禁用已填充的每个字段。

在订阅者中,当我使用:

$childOptions = $child->getConfig()->getOptions();

我收到了 child 的所有已解决选项,我只想获得在表单构建期间通过的选项。 (因为某些 FormTypes(i.o. DocumentType)不可能重新注入(inject)所有已解决的选项,其中一些会导致麻烦)。
  • 表单类型示例:
    class FooType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('entity',EntityType::class,array(
    'class' => 'AppBundle:User',
    'choice_label' => 'username',
    ))
    ->addEventSubscriber($this->changesSubscriber); // See next class
    }
    }
  • 订阅者:
    class ChangesSubscriber implements EventSubscriberInterface
    {
    // Disables filled inputs
    public function postSetData(FormEvent $event)
    {
    $form = $event->getForm();

    foreach($form->all() as $child)
    {
    $childName = $child->getName();
    $childType = $child->getConfig()->getType()->getName();

    // Here I receive all resolved options
    // But I only want the options passed above during 'buildForm' ('class','choice_label') :
    $childOptions = $child->getConfig()->getOptions();

    if(!$child->isEmpty()){
    $form->add($childName,$childType,array_merge($childOptions,array('disabled'=>true)));
    }
    }
    }
    }

  • 这是许多用例的一个例子,另一个例子可能是:
    Alsatian\FormBundle ExtensibleSubscriber

    -> 使 AJAX 提交的选项接受选项/实体/文档类型的表单订阅者。
    此时,如您所见,我选择只采用几个已解决的选项,但我对这个解决方案并不满意。

    最佳答案

    听起来你需要改变你的方法。

    也许做一个自定义表单类型,它的一些选项应该是创建原始类型的选项,类似于 CollectionType作品。

    也许它看起来有点像这样:

    ->add('entity', AjaxType::class,array(
    'ajax_type' => EntityType:class,
    'ajax_options' => [
    'class' => 'AppBundle:User',
    'choice_label' => 'username',
    ]
    ))

    该类型可以添加监听数据并决定做什么的事件。

    关于symfony-forms - 在 FormEvent 中获取 FormField 传递的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263317/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com