Reputation: 63
I search a solution to customize the label of choice of 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'
));
}
}
I know how to modify the DOM to get :
<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>
But I would like get other informations from the Item choices (like property image) :
<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>
Does anyone have a solution?
Upvotes: 1
Views: 1975
Reputation: 10144
Setting a choice_label
is what you're looking for:
$builder->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'choice_label' => 'username',
));
Source: http://symfony.com/doc/current/reference/forms/types/entity.html
If you want to use images in your label, you can customize your form template. You can read about it here:
Upvotes: 1