如何在 symfony 4 中创建自定义表单类型?
How to create custom form type in symfony 4?
我试图通过 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 %}
有趣的是,如果我通过将第一行更改为 {% block entity_widget %}
并在我的表单生成器中使用 EntityType
来通过此模板覆盖 Symfony 的一种内置类型,它会很好地工作.但是当我开始用我自己的自定义类型填充它时,它生气了并显示了很多不相关的错误!
有什么帮助或指导吗?
好的,我知道如何创建它了!
那太容易了。文档显示它很复杂,但实际上并非如此。
这是自定义表单类型文件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
])
...
我在问题中提供的模板正是生成图片的原因!
我试图通过 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 %}
有趣的是,如果我通过将第一行更改为 {% block entity_widget %}
并在我的表单生成器中使用 EntityType
来通过此模板覆盖 Symfony 的一种内置类型,它会很好地工作.但是当我开始用我自己的自定义类型填充它时,它生气了并显示了很多不相关的错误!
有什么帮助或指导吗?
好的,我知道如何创建它了!
那太容易了。文档显示它很复杂,但实际上并非如此。
这是自定义表单类型文件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
])
...
我在问题中提供的模板正是生成图片的原因!