symfony3 表单,无法在 buildForm 方法中获取选项

symfony3 form, can't get options in buildForm method

我真的不知道给这个线程取什么标题,但是,我正在 v3.1.6 中开发一个 Symfony 项目,并在我的表单字段中使用 select2 插件 ajax。

我想使用创建和编辑表单组件的事件(提交和pre_set_data)来动态更改字段,如this part of the symfony doc。一切正常,直到我提交表单并给出错误 Notice: Undefined variable: options

我的表单类型代码在这里

namespace Xxx\ArticleBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Xxx\ArtistBundle\Repository\ArtistRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityManager;

class ArticleType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    //$em = $options['em']; //this give me same error


        $builder
        ->add('artist', EntityType::class, array(
            'class' => 'XxxArtistBundle:Artist',
            'choice_label' => 'artist_name',
            'multiple' => false,
            'expanded' => false))
        ->add('title', TextType::class)
        ->add('categories', EntityType::class, array(
            'class' => 'XxxArticleBundle:Category',
            'choice_label' => 'name',
            'multiple' => true,
            'expanded' => true))
        ->add('image', ChoiceType::class, array(
            'expanded' => false,
            'multiple' => false))
        ->add('intro', CKEditorType::class)
        ->add('content', CKEditorType::class);

    $formModifier = function (FormInterface $form, $image) {
        $listImages = $options['em']->getRepository('XxxAppBundle:Image')->find($image)); //this line give me the error
        if (!$listImages) {
            return; //i will add a FormError in the field
        }
        $listImages = array();
        die(var_dump($listImages));

        $form->add('image', EntityType::class, array(
            'class'       => 'XxxAppBundle:Image',
            'choices'     => $listImages,
        ));
    };

    $builder->get('image')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifier) {
            $image = $event->getForm()->getData();
            //die(var_dump($image)); //select2 field, returned null (but it's not the question
            //die(var_dump($options)); returned error 500 Notice: Undefined variable: options
            $formModifier($event->getForm()->getParent(), $image);
        }
    );
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Xxx\ArticleBundle\Entity\Article',
    ));
    $resolver->setRequired('em');
}

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

我还想说的是,当从仪表板创建 post 时,目标是 UI 与 Wordpress 类似,用于在文章中设置图像以及选择图像时把它放在一个标签中给用户并且没有 entityType 因为它会有千个所以我选择一个 choiceType 与 select2 插件一起使用但是如果有人有更好的解决方案我就可以了。

提前感谢您的帮助

当您在闭包中使用来自父作用域的变量时,您应该将其传递给 'use' 语言结构。

$formModifier = function (FormInterface $form, $image) use ($options) {
    $listImages = $options['em']->getRepository('XxxAppBundle:Image')->find($image)); //this line give me the error
    if (!$listImages) {
        return; //i will add a FormError in the field
    }
    $listImages = array();
    die(var_dump($listImages));

    $form->add('image', EntityType::class, array(
        'class'       => 'XxxAppBundle:Image',
        'choices'     => $listImages,
    ));
};