由于在SF2.6中“Symfony\Component\OptionsResolver\OptionsResolverInterface”已被弃用,我尝试更新我的表单类型:
<?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(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'];
}
现在完成了,没有添加表单选项/更改默认设置。谢谢。
use Symfony\Component\OptionsResolver\OptionsResolver;
。修复方法在此处描述:https://github.com/Soullivaneuh/SonataMediaBundle/commit/c2d688eb891a0f86a375c3f9295659b21f4b91e3 - Joël Salamin