简体   繁体   中英

Silex FormServiceProvider could not load type “form” when using Symfony3 Components

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.4 The PHP micro-framework based on the Symfony Components symfony/security-core
v3.0.1 Symfony Security Component - Core Library symfony/security-csrf v3.0.1 Symfony Security Component - CSRF Library symfony/translation v3.0.1 Symfony Translation Component symfony/twig-bridge v3.0.1 Symfony Twig Bridge symfony/validator v3.0.1 Symfony Validator Component symfony/form v3.0.1

I was able to do this successfully in previous versions of Silex; did something break or am I missing something?

It seems that latest version of Silex does not work with Symfony Forms v3 . To make it work, replace requires in your composer file to:

{
    "require": {
        "silex/silex": "^1.3",
        "symfony/form": "~2.3",
        "symfony/security-csrf": "~2.3"
    }
}

You can actually use Symfony Form 3 with Silex, but createBuilder function now requires you to adhere to the new way of passing in Types as arguments. Silex documentation has not been updated to reflect this.

In pre 2.8, types used to be passed in as strings such as 'form', 'text', 'email' and so on and Symfony components resolved it to the proper class. Now you have to pass in a classname instead.

So, what used to be form now becomes Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType::class . And text becomes Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType::class . Of course, you can import these classes so that you don't have to use the full namespace.

The particular problem posted in this thread can be fixed using the following code:

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();

Please refer to the official documentation .

PS: This is a summary of the discussion from here . I have tested that this works.

Downgrading the Symfony components to version 2.8.1 fixed the problem; this is apparently a backward-compatibility issue between Silex and Symfony3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM