简体   繁体   English

Symfony FormType:如何在表单类型文件中获取原型

[英]Symfony FormType: how to get prototype in form type file

In symfony form type I have a FormType named 在symfony的表单类型我有一个FormType命名

  • KeywordType 关键字类型
  • MessageType 讯息类型
  • ReplyTextMessage ReplyTextMessage
  • ReplyImageMessage ReplyImageMes​​sage

and the code inside look like this 里面的代码看起来像这样

KeywordType 关键字类型

$builder
   ->add('messages', CollectionType::class, [
      'entry_type' => MessageType::class,
      'prototype' => true,
      'allow_add' => true,
      'allow_delete' => true,
      'by_reference' => false,
   )]

MessageType 讯息类型

$builder
   ->add('type', ChoiceType::class, [
      'choices' => array_flip([
         'text' => ReplyMessageInterface::TEXT,
         'image' => ReplyMessageInterface::IMAGE,
      ]),
   )]

Questions is 问题是

How to get Prototype of ReplyTextMessageType and ReplyImageMessageType inside MessageType 如何在MessageType获取ReplyTextMessageTypeReplyImageMessageType原型

because I want to get all of ReplyMessageType in the form_theme 因为我想在form_theme中获取所有form_theme

If you don't understand my question I'll be here to answer any miss understand. 如果您不明白我的问题,我会在这里回答任何小姐的理解。

Thank you for your advance. 谢谢你的进步。

Problem solve! 问题解决!

You must getForm() by used $builder->create() function then set that $builder->create() as an array and set attribute by use $builder->setAttribute() 您必须使用$builder->create() getForm() $builder->create()函数getForm() ,然后将$ builder-> create()设置为array并使用$builder->setAttribute()设置属性

And in function buildView() you just get attribute by use $view->vars['nameAttribute'] and do like my code so your form_theme will have prototype buildView()函数中,您只需使用$view->vars['nameAttribute']获取属性,并像我的代码一样操作,以便您的form_theme将具有原型

for more information please look at my code below 有关更多信息,请查看下面的代码

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('type', ChoiceType::class, [
            'required' => true,
            'choices' => array_flip([
                'text' => self::TEXT_VALUE,
                'sticker' => self::STICKER_VALUE,
                'image' => self::IMAGE_VALUE,
            ]),
        ])
    ;

    $prototypes = [];

    $replyMessageTypes = [
        self::TEXT_VALUE => ReplyTextMessageType::class,
        self::STICKER_VALUE => ReplyStickerMessageType::class,
        self::IMAGE_VALUE => ReplyImageMessageType::class
    ];

    foreach ($replyMessageTypes as $name => $type) {
        $formBuilder = $builder->create(
            $name, $type, []
        );

        $prototypes[$name] = $formBuilder->getForm();
    }

    $builder->setAttribute('replyMessageTypes', $prototypes);
}

/**
 * @param FormView $view
 * @param FormInterface $form
 * @param array $options
 *
 * @return mixed
 */
public function buildView(FormView $view, FormInterface $form, array $options)
{
    parent::buildView($view, $form, $options);

    $view->vars['replyMessageTypes'] = [];

    foreach ($form->getConfig()->getAttribute('replyMessageTypes') as $type => $prototype) {
        /** @var FormInterface $prototype */
        $view->vars['replyMessageTypes'][$type] = $prototype->createView($view);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM