31

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

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
Ferret
  • 1,440
  • 2
  • 12
  • 17

2 Answers2

42

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 */
}
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • 3
    This is working for every class you will create but not for formbuilders, because they inherit from AbstractType, and symfony needs the AbstractType extension to properly create the forms... therefore this does not work in FormBuilders – Nickolaus Feb 24 '15 at 17:03
  • The french [official form doc](http://symfony.com/fr/doc/2.7/book/forms.html) should be updated accordingly – Pierre de LESPINAY Aug 21 '15 at 13:31
  • 1
    Your solution is working well but it may be useful to indicate that the following `use` statement needs to be added: `use Symfony\Component\OptionsResolver\OptionsResolver; `. The fix is described here: https://github.com/Soullivaneuh/SonataMediaBundle/commit/c2d688eb891a0f86a375c3f9295659b21f4b91e3 – Joël Salamin Sep 25 '15 at 06:54
13

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
    ));
}
devilcius
  • 1,764
  • 14
  • 18
  • 1
    Yes already tried that. But then i get the error: 'The option "option1" does not exist. Known options are: "action", "allow_extra_fields", "attr", ....' – Ferret Dec 01 '14 at 10:25
  • As a matter of fact, option1 and options2 don't exist in Symfony's default form options. Did you create them? – devilcius Dec 01 '14 at 10:36
  • 1
    No. "setDefaultOptions(){$resolver->setDefaults([...])}" is the only way i know how to pass additional options/values from my controller to buidForm() cause thats the way its explained in the documentation everywhere. Would you mind telling me how to do that in 2.6? – Ferret Dec 01 '14 at 11:33
  • Check this: http://symfony.com/doc/current/components/options_resolver.html#usage – devilcius Dec 01 '14 at 12:47