在 Symfony 4 中扩展 Core Form BaseType
Extending the Core Form BaseType in Symfony 4
我正在寻找扩展 BaseType 的形式,这就是我所拥有的:
<?php
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\BaseType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
class BaseTypeExtension extends AbstractTypeExtension
{
public static function getExtendedTypes() : iterable
{
return [BaseType::class];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'icon' => null
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['icon'] = $options['icon'];
}
}
?>
问题是当我使用 icon
选项创建表单时,它 returns 出现 The option "icon" does not exist
错误。
问题是,扩展正在注册,如果我使用 php bin/console debug:form
它表明扩展在那里,就像这样:
类型扩展
- App\Form\Extension\BaseTypeExtension
这里缺少什么?
(更新 对于那些可能关心的人:这个答案显然是不正确的)
据我所知,所有表单类型通常都扩展 AbstractType
而不是 BaseType
。我实际上不知道 BaseType
是干什么用的,但可能是为了某些特定的形式。
所以你应该为 AbstractType
写一个扩展,因为所有东西都扩展 AbstractType
。作为示例表单类型扩展(可能与表单扩展混淆):
TransformationFailureExtension
这也是 AbstractType
的扩展。
除此之外,BaseType
source code 中有一条评论:
This type does not appear in the form's type inheritance chain and as such cannot be extended (via {@link \Symfony\Component\Form\FormExtensionInterface}) nor themed.
BaseType 不能像@Jakumi 的回答中指出的那样被扩展,尽管如果你想影响表单生成器,你也不应该扩展 AbstractType。相反,您应该扩展 FormType
,它是任何输入的基础,如 here.
所述
所以总结一下,这就是我的 FormTypeExtension
概述:
<?php
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
class FormTypeExtension extends AbstractTypeExtension
{
public static function getExtendedTypes() : iterable
{
return [FormType::class];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'icon' => null
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['icon'] = $options['icon'];
}
}
?>
它成功了,现在我可以在我的表单构建器输入中使用 "icon" 的扩展。
我正在寻找扩展 BaseType 的形式,这就是我所拥有的:
<?php
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\BaseType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
class BaseTypeExtension extends AbstractTypeExtension
{
public static function getExtendedTypes() : iterable
{
return [BaseType::class];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'icon' => null
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['icon'] = $options['icon'];
}
}
?>
问题是当我使用 icon
选项创建表单时,它 returns 出现 The option "icon" does not exist
错误。
问题是,扩展正在注册,如果我使用 php bin/console debug:form
它表明扩展在那里,就像这样:
类型扩展
- App\Form\Extension\BaseTypeExtension
这里缺少什么?
(更新 对于那些可能关心的人:这个答案显然是不正确的)
据我所知,所有表单类型通常都扩展 AbstractType
而不是 BaseType
。我实际上不知道 BaseType
是干什么用的,但可能是为了某些特定的形式。
所以你应该为 AbstractType
写一个扩展,因为所有东西都扩展 AbstractType
。作为示例表单类型扩展(可能与表单扩展混淆):
TransformationFailureExtension
这也是 AbstractType
的扩展。
除此之外,BaseType
source code 中有一条评论:
This type does not appear in the form's type inheritance chain and as such cannot be extended (via {@link \Symfony\Component\Form\FormExtensionInterface}) nor themed.
BaseType 不能像@Jakumi 的回答中指出的那样被扩展,尽管如果你想影响表单生成器,你也不应该扩展 AbstractType。相反,您应该扩展 FormType
,它是任何输入的基础,如 here.
所以总结一下,这就是我的 FormTypeExtension
概述:
<?php
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
class FormTypeExtension extends AbstractTypeExtension
{
public static function getExtendedTypes() : iterable
{
return [FormType::class];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'icon' => null
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['icon'] = $options['icon'];
}
}
?>
它成功了,现在我可以在我的表单构建器输入中使用 "icon" 的扩展。