Answer the question
In order to leave comments, you need to log in
What is the correct way to use common fields in Symfony forms?
All day I struggle with inheritance in forms - I do everything as it is written here https://symfony.com/doc/current/form/inherit_data_... and nothing comes out. An error is thrown , i.e. supposedly there are no property accessor methods. But they are! I was able to get form field inheritance to work, but not as described in the doc. I will give the existing code (I will reduce it to the maximum, leaving only one property / method for clarity)
First, the model classes
/**
* @ORM\Entity
* @ORM\Table("menus")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({
* "simple" = "MenuItemSimple",
* "page" = "MenuItemPage",
* "article" = "MenuItemArticle",
* "category" = "MenuItemCategory"
* })
*/
class MenuItem
{
/**
* @ORM\Column(type="string", length=256, nullable=true)
*/
private $titleLink;
public function getTitleLink(): ?string
{
return $this->titleLink;
}
}
/**
* @ORM\Entity
*/
class MenuItemPage extends MenuItem {
/**
* @ORM\ManyToOne(targetEntity="Page")
*/
private $page;
public function getEntity(): ?Page
{
return $this->page;
}
}
// ============= класс формы с общими полями
class MenuItemCommonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titleLink', TextType::class, [
'label' => 'label.title',
'required' => false,
])
->add('hrefLink', TextType::class, [
'label' => 'label.href',
'required' => false,
])
->add('iconClass', TextType::class, [
'label' => 'label.icon_class',
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// эту строку добавил как написано на сайте симфони
'inherit_data' => true,
]);
}
}
// ============= теперь класс формы использующей общие поля + одно свое поле
class MenuItemPageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('commonFields', MenuItemCommonType::class, [
'data_class' => MenuItemPage::class,
])
->add('entity', Select2EntityType::class, [
'label' => 'label.assigned_page',
'multiple' => false,
'remote_route' => 'admin_page_ajax_searching',
'class' => Page::class,
'placeholder' => 'Select a page',
'required' => false,
'remote_params' => [
'entityClass' => urlencode(Page::class),
'entityId' => $options['data']->getId(),
],
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MenuItemPage::class,
]);
}
}
class MenuItemPageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ->add('commonFields', MenuItemCommonType::class, [
// 'data_class' => MenuItemPage::class,
// ])
->add('entity', Select2EntityType::class, [
'label' => 'label.assigned_page',
'multiple' => false,
'remote_route' => 'admin_category_ajax_searching',
'class' => Page::class,
'placeholder' => 'Select a page',
'required' => false,
'remote_params' => [
'entityClass' => urlencode(Page::class),
'entityId' => $options['data']->getId(),
],
])
;
}
public function getParent()
{
return MenuItemCommonType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MenuItemPage::class,
]);
}
}
// а это кусок из класса с общими полями
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// 'inherit_data' => true,
]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question