Ferret
Ferret

Reputation: 1440

SF2.6 OptionsResolverInterface deprecated and AbstractType:setDefaultOptions

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

Upvotes: 31

Views: 12005

Answers (2)

Nickolaus
Nickolaus

Reputation: 4835

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 */
}

Upvotes: 42

devilcius
devilcius

Reputation: 1884

Have you considered using configureOptions function, instead of setDefaultOptions:

protected function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
            'data_class' => 'xxx',
            'option1' => [],
            'option2' => 3,
            'intention' => 'xxx',
            'cascade_validation' => true
    ));
}

Upvotes: 13

Related Questions