SF2.6 选项解析接口已弃用和抽象类型:setDefaultOptions
2022-08-30 14:54:20
由于“Symfony\Component\OptionsResolver\OptionsResolverInterface”在SF2.6中被弃用,我试图更新我的FormTypes:
<?php
namespace Xxx\XxxBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @uses Symfony\Component\Form\AbstractType
* @uses Symfony\Component\Form\FormBuilderInterface
* @uses Symfony\Component\OptionsResolver\OptionsResolver
* @package Xxx\XxxBundle\Form\Type
*/
class XxxType extends AbstractType
{
/**
* default form builder
*
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('xxx', 'text') // ...
}
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => 'xxx',
'option1' => [],
'option2' => 3,
'intention' => 'xxx',
'cascade_validation' => true
]
);
}
/**
* @return string
*/
public function getName()
{
return 'xxx';
}
}
问题在于,AbstractType仍然期望“setDefaultOptions(OptionsResolverInterface $resolver)”而不是“OptionsResolover”
声明必须与 FormTypeInterface->setDefaultOptions 兼容(解析器: \Symfony\Component\OptionsResolver\OptionsResolverInterface)
这里有什么遗漏吗?
谢谢;)
编辑
更改了我的控制器调用
$form = $this->createForm(
new XxxType(),
$xxxEntity,
[
'option1' => $array
]
);
自
$form = $this->createForm(
new XxxType([
'option1' => $array
]),
$xxxEntity
);
并将其添加到表单类型中:
protected $option1;
public function __construct($options)
{
$this->option1 = $options['option1'];
}
做到了,现在没有添加表单选项/更改默认值。谢谢