Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在symfony 4中创建自定义表单类型?_Php_Symfony_Symfony4_Custom Field Type - Fatal编程技术网

Php 如何在symfony 4中创建自定义表单类型?

Php 如何在symfony 4中创建自定义表单类型?,php,symfony,symfony4,custom-field-type,Php,Symfony,Symfony4,Custom Field Type,我试图通过自己的模板视图创建一个自定义表单类型,但通过搜索并尝试创建一个,我遇到了很多错误 这是我的自定义表单类型文件ImageChoiceType.php: <?php namespace App\Form\Type; use App\Entity\Media; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component

我试图通过自己的模板视图创建一个自定义表单类型,但通过搜索并尝试创建一个,我遇到了很多错误

这是我的自定义表单类型文件
ImageChoiceType.php

<?php

namespace App\Form\Type;

use App\Entity\Media;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ImageChoiceType extends AbstractType
{

    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }


    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
//            'data_class' => Media::class,
            'class' => Media::class,
            'choices' => $this->entityManager->getRepository(Media::class)
                            ->findAll(),
        ));
    }

    public function getParent()
    {
       return EntityType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'image_choice';
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'image_choice';
    }
}
<?php

namespace App\Form\Type;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ImageChoiceType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
    }

    public function getParent()
    {
        return EntityType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'image_choice';
    }

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

好的,我找到了创建它的方法
那太容易了。文档显示它非常复杂,但事实并非如此

这是自定义表单类型文件
ImageChoiceType.php

<?php

namespace App\Form\Type;

use App\Entity\Media;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ImageChoiceType extends AbstractType
{

    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }


    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
//            'data_class' => Media::class,
            'class' => Media::class,
            'choices' => $this->entityManager->getRepository(Media::class)
                            ->findAll(),
        ));
    }

    public function getParent()
    {
       return EntityType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'image_choice';
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'image_choice';
    }
}
<?php

namespace App\Form\Type;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ImageChoiceType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
    }

    public function getParent()
    {
        return EntityType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'image_choice';
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'image_choice';
    }
}
我在问题中提供的模板正是生成图片的模板