1

I am working on an application based on Symfony 2.7. I have a custom form type containing the following code:

namespace MyCompany\AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class,
                [
                    'attr'=>
                        [
                            'placeholder' => 'Your name'
                        ],
                    'constraints' =>
                        [
                            new NotBlank(['message' => 'Please provide your name'])
                        ]
                ]
            )
        ;
    }
...

... and when I load the form, I get the following InvalidArgumentException:

Could not load type "Symfony\Component\Form\Extension\Core\Type\TextType"

I have verified that the TextType class exists.

I tried using composer dump, and it didn't seem to help. In addition, I tried removing the vendor directory and redoing composer install, and that didn't help either.

What else can I try?

2
  • You can run composer dump in order to refresh the vendors cache, or remove the vendor/ directory like someone did. Commented Sep 26, 2018 at 9:26
  • Thanks for your comment, AL. I just tried using composer dump, and it didn't seem to help. I also just tried removing the vendor directory and redoing composer install, and that didn't help either. Commented Sep 26, 2018 at 9:39

1 Answer 1

3

You can't use fully qualified class names to denote form types in Symfony v2.7 - that was added in v2.8. You need to denote your types by passing an instance instead:

$builder
    ->add('name', new TextType(),
        [
            'attr'        =>
                [
                    'placeholder' => 'Your name',
                ],
            'constraints' =>
                [
                    new NotBlank(['message' => 'Please provide your name']),
                ],
        ]
    );

Or by using the shorthand name e.g text:

$builder
    ->add('name', 'text',
        [
            'attr'        =>
                [
                    'placeholder' => 'Your name',
                ],
            'constraints' =>
                [
                    new NotBlank(['message' => 'Please provide your name']),
                ],
        ]
    );

Symfony v2.7 isn't maintained anymore though, so I'd highly recommend upgrading to at least v2.8

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.