12
votes

I'm using Symfony Standard Edition, and everything work in the version of Symfony2.X until I update it to the 3.0.x-dev.

Even in the newer version, everything works except a page that give me an error in the Controller:

Could not load type "text" 500 Internal Server Error - InvalidArgumentException

  1. in vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php at line 91
  2. at FormRegistry ->getType ('text') in vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 84
  3. at FormFactory ->createNamedBuilder ('flag', 'text', null, array()) in vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 106
  4. at FormBuilder ->create ('flag', 'text', array()) in vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 267
  5. at FormBuilder ->resolveChildren () in vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 215
  6. at FormBuilder ->getForm () in src/MyProject/FrontOfficeBundle/Controller/ChallengeController.php at line 418

In the Controller I used this code:

$form = $this->createFormBuilder()
->add("flag","text")
->add("validate","button")
->getForm();

Even if I delete the first add("flag","text"), the error switch to:

Could not load type "button"

So I think that the problem is in the method getForm(). I gassed that the method createFormBuilder() need a parameter so I tried to pass an object Flag which it have many arguments (flag,validate,...).

The problem didn't change it's like the syntax was changed in this version but when I verified the documentation, I didn't found any problems in my syntax.

The form's version is 3.0-dev. I verified it in the github project and those files are the latests. I used

composer update

And I removed the cache and the logs files but problem exists.

Thanks for your help and sorry for my bad english.

2
Please notice that the documentation for the 3.0 branch is not at the link you sent, but rather here: symfony.com/doc/master/book/forms.html. You can change the version of the documentation using the dropdown you will find at the top right of the pageCarlos Granados
Also make sure to read the UPGRADE-3.0.md document in your Symfony installation to get a list of changes needed to upgrade to 3.0Carlos Granados
Thanks for your answer, yes I mean this link, but it's the same. I haven't upgrade my project in the composer, I have download the master version (3.0.x-dev) of the symfony and I placed my project in the src. Then I installed and update everything with the composer so I haven't the UPGRADE-3.0.md. All my pages works except this pageEmpereur Paradis Aymen

2 Answers

25
votes

What @CarlosGranados means by read the UPGRADE-3.0.md is to read the file that tells you how you would need to change your code from that of 2.x to 3.0 rather than how to upgrade the symfony code base. Unfortunately it doesn't mention how to deal with this forms issue.

The issue that you will be facing is covered in the [UPGRADE-2.8.md](https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md) and is due to the form names text/button being deprecated in favour of their fully-qualified class name (FQCN).

From UPGRADE-2.8.md

Type names were deprecated and will be removed in Symfony 3.0. Instead of referencing types by name, you should reference them by their fully-qualified class name (FQCN) instead. With PHP 5.5 or later, you can use the "class" constant for that:

Before:

$form = $this->createFormBuilder()
    ->add('name', 'text')
    ->add('age', 'integer')
    ->getForm();

After:

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

$form = $this->createFormBuilder()
    ->add('name', TextType::class)
    // or ->add('name', Symfony\Component\Form\Extension\Core\Type\TextType)
    ->add('age', IntegerType::class)
    // or ->add('age', Symfony\Component\Form\Extension\Core\Type\IntegerType)
    ->getForm();

... and it goes on to tell you a bunch more ...

2
votes

Our webserver is still at php 5.3 - so I'm having to find workarounds for symfony3 as I cant use TextType::class on our live server.

You can minimise your code on large forms by setting a constant to the class reference:

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

const textType = 'Symfony\Component\Form\Extension\Core\Type\TextType';
const emailType = 'Symfony\Component\Form\Extension\Core\Type\EmailType';

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', textType, array('label' => 'Username'))
            ->add('address1', textType, array('label' => 'Address1'))
            ->add('address2', textType, array('label' => 'Address2'))
            ->add('postcode', textType, array('label' => 'Postcode'))
         ;
    }

This works for me!