1

this is my form:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class JoseType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombre')
            ->add('apellido')
            ->add('edad')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Jose'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_jose';
    }
}

And this is my test:

use AppBundle\Form\JoseType;
use Symfony\Component\Form\Test\TypeTestCase;

class ProjectTypeTest extends TypeTestCase
{
    public function testForm()
    {
         $form = $this->factory->create(JoseType::class);

    }
}

To running test receive this:

Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "AppBundle\Form\JoseType"

I have symfony 2.6.13.

In symfony 3.2 works, but 2.6 doesn't work, Why? ... any idea?

1
  • Clear the cache (php app/console cache:clear [--env=prod --no-debug]), are you using composers autoload map? If so, try dumping it again (composer dump-autoload -a). Commented May 24, 2017 at 20:58

1 Answer 1

1

The first problem is that in symfony 2.6 you should make instance of your type and pass it to create function.

$type = new JoseType();  $form = this->factory->create($type);

Code you wrote is valid since version 2.8.

The second problem is in

public function setDefaultOptions(OptionsResolverInterface $resolver)

since symfony 2.7 you should use

public function configureOptions(OptionsResolver $resolver) 

instead setDefaultOptions function. So your code will work in symfony 2.6 and will not in symfony 3.2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.