我在buildForm中有以下代码,我想获得Collection<Object>
的翻译,但我找不到方法。有可能吗?
if(Groups.GroupBy(e=>e.GroupName).Count()>1)
{
//...
}
答案 0 :(得分:1)
尝试明确设置translation_domain
:
$builder->add('subject', TextType::class, array(
// ...
'translation_domain' => 'messages',
))
您还可以通过将此方法添加到:{/ p>来为整个FormType
执行此操作
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'translation_domain' => 'messages'
));
}
如果它不起作用,请通过注册as a service translator
注入FormType
{/ 1}}:
# services.yml
services:
app.form.type.abuse:
class: AppBundle\Form\Type\AbuseType
arguments: [ "@translator" ]
tags:
- { name: form.type }
直接致电翻译:
use Symfony\Component\Translation\TranslatorInterface;
class AbuseType extends AbstractType
{
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
// ...
$builder->add('subject', TextType::class, array(
'label' => 'label.subject',
'data' => $this->translator->trans('report_abuse', array(), 'messages'),
'disabled' => 'true',
))
}
答案 1 :(得分:0)
所以这就是我所做的。我已经在滥用类中注入了主题,因为默认情况下它的值将用于构建表单。我还将字段设置为readonly而不是disabled,这会导致字段未提交。
//FormController.php
class FormController extends Controller
{
//...
public function abuseAction(Request $request)
{
$subject = $this->get('translator')->trans('report_abuse');
$abuse = new Abuse($subject);
//...
}
//...
}
//Abuse.php
class Abuse
{
//...
public function __construct($subject)
{
$this->setSubject($subject);
}
//...
}
//AbuseType.php
class AbuseType extends AbstractType
{
//...
$builder->add('subject', TextType::class, array(
'label' => 'label.subject',
'attr' => array(
'readonly' => true,
)))
//...
}