Oksana
Oksana

Reputation: 33

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

I'm passing a course on OpenClassrooms.

I create a simple form.

Controller:

// src/OC/PlatformBundle/Controller/AdvertController.php

namespace OC\PlatformBundle\Controller;

use OC\PlatformBundle\Entity\Advert;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AdvertController extends Controller
{
  public function addAction(Request $request)
  {
    // On crée un objet Advert
    $advert = new Advert();

    // On crée le FormBuilder grâce au service form factory
    $formBuilder = $this->get('form.factory')->createBuilder('form', $advert);

    // On ajoute les champs de l'entité que l'on veut à notre formulaire
    $formBuilder
      ->add('date',      'date')
      ->add('title',     'text')
      ->add('content',   'textarea')
      ->add('author',    'text')
      ->add('published', 'checkbox')
      ->add('save',      'submit')
    ;
    // Pour l'instant, pas de candidatures, catégories, etc., on les gérera plus tard

    // À partir du formBuilder, on génère le formulaire
    $form = $formBuilder->getForm();

    // On passe la méthode createView() du formulaire à la vue
    // afin qu'elle puisse afficher le formulaire toute seule
    return $this->render('OCPlatformBundle:Advert:add.html.twig', array(
      'form' => $form->createView(),
    ));
      }
    }

View:

{# src/OC/PlatformBundle/Resources/views/Advert/form.html.twig #}

<h3>Formulaire d'annonce</h3>

<div class="well">
  {{ form(form) }}
</div>

And I'm getting the error: Could not load type "form" 500 Internal Server Error - InvalidArgumentException

*The course is written for Symfony2, I'm using Symfony3.

What type "form" can not it load? What can be the problem?

Upvotes: 1

Views: 2980

Answers (3)

Oksana
Oksana

Reputation: 33

So the solution was to read the SymfonyBook for Symfony3.

The code became:

//...
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

//...

public function addAction(Request $request)
  {

    $advert = new Advert();

    $form = $this->createFormBuilder($advert)   
      ->add('date',      DateType::class)
      ->add('title',     TextType::class)
      ->add('content',   TextareaType::class)
      ->add('author',    TextType::class)
      ->add('published', CheckboxType::class)
      ->add('save',      SubmitType::class, array('label' => 'Create Add'))
      ->getForm();

    return $this->render('OCPlatformBundle:Advert:add.html.twig', array(
      'form' => $form->createView(),
    ));

  }

The code of the template has not changed.

Upvotes: 1

tftd
tftd

Reputation: 17062

You shouldn't be using Symfony 3 to begin with. The course clearly stated it was meant for Symfony 2

The exception you're receiving is probably due to the changes in the Forms. In Symfony 3 you need to use FQCN (fully-qualified class name) instead of an instance of the form (or a string reference to a service). In other words, your code should look something like this:

namespace OC\PlatformBundle\Controller;

use OC\PlatformBundle\Entity\Advert;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

// Include the FormType you'll be using. 
// If you have a custom FormType, include that one i.e ContactType, MemberType, etc.
// and then in the `createBuilder` method replace "FormType" with "ContactType" (or whatever the name of the form type class is)
use Symfony\Component\Form\Extension\Core\Type\FormType; 

class AdvertController extends Controller
{
    public function addAction(Request $request)
    {
        ....
        $formBuilder = $this->get('form.factory')->createBuilder(FormType::class, $advert);
        ....
    }
}

There are some other backwards incompatible changes therefore I advice you to use Symfony2 while going through your course. Otherwise you'll bump into more exceptions/errors and you'll be wasting time chasing your tail instead of actually learning. :)

Upvotes: 4

Mateusz Sip
Mateusz Sip

Reputation: 1280

If you pass two arguments to a createBuilder, the first argument should be a FQDN for a form type. In this case try with: $formBuilder = $this->get('form.factory')->createBuilder($advert);

Upvotes: 0

Related Questions