由于“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)”而不是“OptionsResolver”
声明必须与 FormTypeInterface->setDefaultOptions(resolver : \Symfony\Component\OptionsResolver\OptionsResolverInterface) 兼容
我这里有什么遗漏吗?
谢谢 ;)
编辑
更改了我的控制器调用
$form = $this->createForm(
new XxxType(),
$xxxEntity,
[
'option1' => $array
]
);
至
$form = $this->createForm(
new XxxType([
'option1' => $array
]),
$xxxEntity
);
并将其添加到 FormType:
protected $option1;
public function __construct($options)
{
$this->option1 = $options['option1'];
}
做到了,现在没有添加表单选项/更改默认值。谢谢