SF2.6 OptionsResolverInterface deprecated and AbstractType:setDefaultOptions

Ferret picture Ferret · Dec 1, 2014 · Viewed 11.6k times · Source

since "Symfony\Component\OptionsResolver\OptionsResolverInterface" is deprecated in SF2.6 I tried to update my 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';
    }
}

The problem is that AbstractType still expects "setDefaultOptions(OptionsResolverInterface $resolver)" instead of "OptionsResolover"

Declaration must be compatible with FormTypeInterface->setDefaultOptions(resolver : \Symfony\Component\OptionsResolver\OptionsResolverInterface)

Is there anything im missing here?

Thanks ;)


EDIT

Changed my controller call from

$form = $this->createForm(
    new XxxType(),
    $xxxEntity,
    [
        'option1' => $array
    ]
);

to

$form = $this->createForm(
    new XxxType([
        'option1' => $array
    ]),
    $xxxEntity
);

and adding this to the FormType:

protected $option1;
public function __construct($options)
{
    $this->option1 = $options['option1'];
}

did it, without adding form options / changing defaults now. Thanks

Answer

Nickolaus picture Nickolaus · Feb 23, 2015

In version 2.6 there is no real replacement for this function inside the FormBuilder
Therefore if using version 2.6. it can still be used...
however
In symfony version 2.7 the function

public function setDefaultOptions(OptionsResolverInterface $resolver)

has been replaced by:

public function configureOptions(OptionsResolver $resolver)

in order to provide downgrade functionality this is the way to go:

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    /** @var OptionResolver $resolver */
    $this->configureOptions($resolver);
}

public function configureOptions(OptionsResolver $resolver) {
     /* define your defaults here */
}