In symfony form type I have a FormType
named
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
because I want to get all of ReplyMessageType in the 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()
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
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.