8000 [Form][Bug][2.8.1] Bug (The required option "class" is missing) when using CollectionType with option 'entry_type' => EntityType::class · Issue #17299 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Form][Bug][2.8.1] Bug (The required option "class" is missing) when using CollectionType with option 'entry_type' => EntityType::class #17299
Closed
@AlexandreHagen

Description

@AlexandreHagen

Hi !

Under Symfony 2.8.0 everything are working. But with Symfony 2.8.1 I got this error:

The required option "class" is missing in vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 779

I have compared 2.8.0 vs 2.8.1 modifications (link to compare!), and maybe issue comes from CollectionType.php. But I can be sure.

Could you please check if 2.8.1 introduce a new bug or help me to close this issue?

Thanks !

Below my FormType:

<?php

namespace Freedom\DemophonieBundle\Form;

use Doctrine\ORM\EntityRepository;
use Freedom\DemophonieBundle\Entity\UserList;
use Freedom\UserBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class UserListType
 * @package Freedom\DemophonieBundle\Form
 */
class UserListType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'name',
            TextType::class,
            [
                'required' => true,
                'description' => 'Nom de la liste',
            ]
        )->add(
            'visibility',
            ChoiceType::class,
            [
                'required' => true,
                'description' => 'Visibilité de la liste',
                'choices' => ['private' => UserList::VISIBILITY_PRIVATE, 'internal' => UserList::VISIBILITY_INTERNAL],
            ]
        )->add(
            'image',
            TextType::class,
            [
                'required' => false,
                'description' => 'Image de la liste en Base64',
                'mapped' => false,
                'empty_data' => UserList::DEFAULT_LOGO,
            ]
        )->add(
            'users',
            CollectionType::class,
            [
                'required' => true,
                'description' => 'Id des utilisateurs à ajouter à la liste',
                'allow_add' => true,
                'entry_type' => EntityType::class,
                'entry_options' => [
                    'class' => User::class,
                    'query_builder' => function (EntityRepository $er) use ($options) {
                        return $er->createQueryBuilder('u')
                            ->from('FreedomUserBundle:UserSpace', 'us')
                            ->where('us.space = :space')
                            ->andWhere('us.user = u')
                            ->setParameter('space', $options['space']);
                    },
                    'choice_label' => 'id',
                    'invalid_message' => "L'utilisateur n'existe pas ou n'est pas dans votre espace",
                ],
            ]
        );
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => UserList::class,
                'space' => null,
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return $this->getBlockPrefix();
    }

    /**
     * {@inheritDoc}
     */
    public function getBlockPrefix()
    {
        return '';
    }
}

And the function that use FormType is:

public function postListAction(Request $request, GeneralSpace $space)
    {
        $user = $this->getUser();
        $avatarHelper = $this->get('vich_uploader.templating.helper.uploader_helper');

        $this->get('freedom.access_control')->haveAccess($user, $space);

        $userList = new UserList();
        $userList->setUser($user)->setSpace($space);

        $form = $this->createForm(UserListType::class, $userList, ['space' => $space]);

        $form->handleRequest($request);

        if ($form->isValid()) {
        ....
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0