我真的不知道给这个帖子提供哪个标题但是,我正在使用v3.1.6中的Symfony项目,并使用ajax在我的表单字段中使用select2插件。
我想使用表单组件的事件(submit和pre_set_data)进行创建和编辑,以便像this part of the symfony doc一样动态更改字段。一切正常,直到我提交表格并给出错误注意:未定义的变量:选项
我的表单类型代码在这里
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';
}
}
我还想说,目标是在从仪表板创建帖子时使用与Wordpress相似的UI,在文章中设置图像,并在选择图像时将其标记为用户,无需entityType因为它会有数千个,所以我选择一个choiceType与select2插件一起使用,但是如果有人得到了更好的解决方案我就可以了。
提前帮助
答案 0 :(得分:0)
当您在闭包内使用父作用域中的变量时,应将其传递给'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,
));
};