Thanks to visit codestin.com
Credit goes to github.com

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AlexandreHagen opened this issue Jan 7, 2016 · 4 comments

Comments

@AlexandreHagen
Copy link

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()) {
        ....
}
@wouterj
Copy link
Member

wouterj commented Jan 7, 2016

This is indeed a bug on Symfony 2.8.1 and this will be fixed in 2.8.2. Please use 2.8.0 or 2.8-dev for now.

@AlexandreHagen
Copy link
Author

Thanks @wouterj! I will do that. But I am a curious guy and I wish to understand that bug (I tried to solve this problem for three hours). Could you tell me in which class are involved? There are already a commit or a pull request to fix that bug?

@wouterj
Copy link
Member

wouterj commented Jan 7, 2016

@AlexandreHagen the bug was fixed in this PR: #17162 and introduced in #17044

@AlexandreHagen
Copy link
Author

Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants