【发布时间】:2016-05-03 15:12:43
【问题描述】:
我搜索了一个解决方案来自定义 EntityType 的选择标签。
实体
发布
class Post
{
// ...
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Item", cascade={"persist"})
*/
private $items;
// ...
}
项目
class Item
{
// ...
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=127)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;
// ...
public function __toString(){
return $this->title;
}
}
表格
class PostType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('items', EntityType::class, array(
'class' => 'AppBundle\Entity\Item',
'multiple' => true,
'expanded' => true,
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Post'
));
}
}
结果
我知道如何修改 DOM 来获取:
<ul>
<li>
<input type="checkbox" id="post_items_1" name="post[items][]" value="1">
<label for="post_items_1">Item 1</label>
</li>
<li>
<input type="checkbox" id="post_items_2" name="post[items][]" value="2">
<label for="post_items_2">Item 2</label>
</li>
<!-- ... -->
</ul>
但我想从项目选择中获取其他信息(如属性图像):
<ul>
<li>
<input type="checkbox" id="post_items_1" name="post[items][]" value="1">
<label for="post_items_1">
Item 1
<img src="/uploads/item/lorem.jpg" alt="" /> <!-- path store into item #1 -->
</label>
</li>
<li>
<input type="checkbox" id="post_items_2" name="post[items][]" value="2">
<label for="post_items_2">
Item 2
<img src="/uploads/item/ipsum.jpg" alt="" /> <!-- path store into item #2 -->
</label>
</li>
<!-- ... -->
</ul>
有人有解决办法吗?
【问题讨论】:
标签: symfony symfony-forms