silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

Silex FormServiceProvider could not load type “form” when using Symfony 3 components #1302

Closed bestattendance closed 8 years ago

bestattendance commented 8 years ago

When trying to build a form I receive this error:

InvalidArgumentException in FormRegistry.php line 87: Could not load type "form"

I've registered the FormServiceProvider, TranslationServicerProvider, and ValidatorServiceProvider.

Here is the relevant part of my code:

    $this->_form = $this->_app['form.factory']->createBuilder('form', $this->_map())
    ->add('firstName', 'text', [
        'constraints' => [new Assert\NotBlank()]
    ])
    ->add('lastName', 'text', [
        'constraints' => [new Assert\NotBlank()]
    ])
    ->add('email', 'text', [
        'constraints' => [new Assert\Email()]
    ])
    ->getForm();

Here are version numbers of the related components that I'm using:

silex/silex v1.3.5 symfony/security-core v3.0.1 symfony/security-csrf v3.0.1 symfony/translation v3.0.1 symfong/twig-bridge v3.0.1 symfony/validator v3.0.1 symfony/form v3.0.1

Downgrading all symfony components to 2.8.1 solved the problem.

bestattendance commented 8 years ago

Ref: http://stackoverflow.com/questions/34663227/silex-formserviceprovider-could-not-load-type-form

hellboy1975 commented 8 years ago

I can confirm this problem. Downgrading the Symfony components was the only option I could find that worked.

HeahDude commented 8 years ago

This is not a FormServiceProvider issue. It's about one of the few bc break introduced in version 3 of symfony components. You can get this working by changing the deprecated alias form and text by FormType::class and TextType::class :

use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

$this->_form = $this->_app['form.factory']->createBuilder('FormType::class', $this->_map())
    ->add('firstName', 'TextType::class', [
        'constraints' => [new Assert\NotBlank()]
    ])
    ->add('lastName', 'TextType::class', [
        'constraints' => [new Assert\NotBlank()]
    ])
    ->add('email', 'TextType::class', [
        'constraints' => [new Assert\Email()]
    ])
    ->getForm();
JonathanRamier commented 8 years ago

Hello @HeahDude

You forgot to remove quote in your example

use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;

$this->_form = $this->_app['form.factory']
    ->createBuilder(FormType::class, $this->_map())
    ->add(
        'firstName', 
        TextType::class, [
            'constraints' => [new Assert\NotBlank()]
        ]
    )
    ->add(
        'lastName', 
        TextType::class, [
             'constraints' => [new Assert\NotBlank()]
        ]
     )
    ->add(
         'email', 
         EmailType::class, [
              'constraints' => [new Assert\Email()]
         ]
    )
    ->getForm();

see doc

HeahDude commented 8 years ago

You're right @JonathanRamier, I've mistaken after copy-pasting, thank you !