3

我试图通过Symfony 4 Documentation使用它自己的模板视图创建一个自定义表单类型,但是通过搜索和尝试创建一个我得到了很多错误。

这是我的自定义表单类型文件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';
    }
}

这是我的字段模板:

{% block image_choice_widget %}
    <div class="image_widget">
        <div class="radio">
            <label for="post_image_placeholder" class="">
                <input type="radio" id="post_image_placeholder" name="post[image]" value=""
                        {{ form.vars.value == "" ? "checked='checked'" : ""}}> None
            </label>
        </div>
        <div class="image-container">
            {% for key,choice in form.vars.choices %}
                <div class="radio col-xs-2">
                    <label for="post_image_{{ key }}" class="">
                        <input class="image-radio-buttons" type="radio" id="post_image_{{ key }}" name="post[image]"
                               value="{{ key }}" {{ form.vars.value == key ? "checked='checked'" : ""}}>
                        <img src="{{ asset(constant('App\\Constants::UPLOAD_MEDIA')~choice.label) }}" class="image-thumbs img-responsive"/>
                    </label>
                </div>
            {% endfor %}
            <div class="clearfix"></div>
        </div>
    </div>
{% endblock %}

有趣的部分是,如果我通过这个模板覆盖其中一个内置类型的 Symfony,通过将第一行更改为{% block entity_widget %}EntityType在我的表单构建器中使用,它运行良好。但是当我开始用我自己的自定义类型填充它时,它很生气并显示了很多不相关的错误!

有什么帮助或指导吗?

4

1 回答 1

3

好的,我发现了如何创建它!
那太容易了。文档显示它是如此复杂,但实际上并非如此。

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

<?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';
    }
}

这就是我在表单构建器中使用此字段的方式:

...
  ->add('image',ImageChoiceType::class , [
       'label' => 'Choose an Image',
       'class' => Media::class
  ])
...

我在问题中提供的模板正是生成图片的原因!

于 2018-04-07T08:00:25.720 回答