简体   繁体   English

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

[英]How to create custom form type in symfony 4?

I was trying to create a custom form type with it's own template view via Symfony 4 Documentation but I got a lot of errors by searching and trying to create one. 我试图通过Symfony 4 Documentation创建一个带有自己的模板视图的自定义表单类型,但是通过搜索并尝试创建一个错误我遇到了很多错误。

Here is my custom form type file ImageChoiceType.php : 这是我的自定义表单类型文件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';
    }
}

Here is my field template: 这是我的字段模板:

{% 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 %}

The interesting part is if I override one of the built-in types of Symfony via this template by changing the first line to {% block entity_widget %} and use EntityType in my form builder, it works well. 有趣的是,如果我通过这个模板覆盖一个内置类型的Symfony,通过将第一行更改为{% block entity_widget %}并在我的表单构建器中使用EntityType ,它运行良好。 But as I started to populate this with my own custom type, It got angry and showed a lot of unrelated errors! 但是当我开始使用我自己的自定义类型填充它时,它生气并显示了许多无关的错误!

Any help or instruction? 任何帮助或指导?

OK, I found out how to create it! 好的,我发现了如何创建它!
That was so easy. 那太容易了。 The documentation showed that it's so complex, but actually it's not. 文档显示它非常复杂,但事实并非如此。

This is the custom form type file ImageChoiceType.php : 这是自定义表单类型文件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';
    }
}

This is how I use this field in my form builder: 这是我在表单构建器中使用此字段的方式:

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

and the template that I've provided in the question is exactly what generates the pictures! 我在问题中提供的模板正是生成图片的原因!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM