将其显示为%name% (%capacity% places)
表单视图中的“可能”表示形式,因此我会将这种非常具体的表示形式转换为您的表单类型。
您的Venue实体的__toString()
方法中可以包含哪些内容:
class Venue
{
private $name;
... setter & getter method
public function __toString()
{
return $this->getName();
}
}
消息.en.yml:
my_translation: %name% (%capacity% places)
接下来您的表单类型使用choice_label(也值得了解:choice_translation_domain):
use Symfony\Component\Translation\TranslatorInterface;
class YourFormType extends AbstractType
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'venue',
EntityType::class,
array(
'choice_label' => function (Venue $venue, $key, $index) {
// Translatable choice labels
return $this->translator->trans('my_translation', array(
'%name%' => $venue->getName(),
'%capacity%' => $venue->getCapacity(),
));
}
)
);
}
}
& 还将您的表单类型注册为 services.yml 中的服务:
your_form_type:
class: Your\Bundle\Namespace\Form\YourFormType
arguments: ["@translator"]
tags:
- { name: form.type }