Symfony:在子窗体上设置自定义数据属性以在树枝模板中使用
Symfony: set custom data attribute on child form for use in twig template
我有以下设置。
- 一天的实体
- 有自己字段的日表格
我想向 Day 表单添加一个基于六个 Day 属性的计算 'total' 值。我已经成功地创建了一个 class 这样做。所以主要问题是:如何link将计算出的数据转为每天的表格。
我在下面只显示了必要的代码,所以如果您遗漏了什么,它不会显示。
DayEntity 是默认的,有一堆字段。
DayType 形式:
class DayType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field');
// etc, bunch of fields
// Here I want to add a calculated field. How to do it?
// The result is retrieved from my service, with a function: calculateTotal($day);
}
}
I have already a service which does the necessary computation:
public function calculateTotal(Day $day)
{
// Some magic here ;-)
return $returnDateInterval;
}
在 Twig 中我喜欢做这样的事情:
{{ dayform.vars.data.calculatedTotal|date('H:i') }}
// or
{{ dayform.calculatedTotal|date('H:i') }}
我已经在……伟大的 Symfony 文档中找到了答案! Defining your forms as services
所以我所做的就是修改我的 service.yml:
app.subscriber.calculatetotal:
class: AppBundle\Form\EventListener\CalculateTotalFieldSubscriber
arguments: ["@app.manager.day"]
app.form.day:
class: AppBundle\Form\Type\DayType
arguments: ["@app.subscriber.calculatetotal"]
tags:
- { name: form.type }
如您所见,我还添加了一个 Form EventListener,它监听 PRE_SUBMIT 和 POST_SET_DATA 以更改我的非映射 'total' 字段。
在DayType.php表格中:
private $calculateTotalFieldSubscriber;
public function __construct(CalculateTotalFieldSubscriber $calculateTotalFieldSubscriber)
{
$this->calculateTotalFieldSubscriber = $calculateTotalFieldSubscriber;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// I do not save this field to the database, so mapped is set to false
->add('total', TimeType::class, array(
'mapped' => false
))
$builder->addEventSubscriber($this->calculateTotalFieldSubscriber);
}
calculateTotalFieldSubscriber是默认的,监听两个事件。
我有以下设置。 - 一天的实体 - 有自己字段的日表格
我想向 Day 表单添加一个基于六个 Day 属性的计算 'total' 值。我已经成功地创建了一个 class 这样做。所以主要问题是:如何link将计算出的数据转为每天的表格。
我在下面只显示了必要的代码,所以如果您遗漏了什么,它不会显示。
DayEntity 是默认的,有一堆字段。
DayType 形式:
class DayType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field');
// etc, bunch of fields
// Here I want to add a calculated field. How to do it?
// The result is retrieved from my service, with a function: calculateTotal($day);
}
}
I have already a service which does the necessary computation:
public function calculateTotal(Day $day)
{
// Some magic here ;-)
return $returnDateInterval;
}
在 Twig 中我喜欢做这样的事情:
{{ dayform.vars.data.calculatedTotal|date('H:i') }}
// or
{{ dayform.calculatedTotal|date('H:i') }}
我已经在……伟大的 Symfony 文档中找到了答案! Defining your forms as services
所以我所做的就是修改我的 service.yml:
app.subscriber.calculatetotal:
class: AppBundle\Form\EventListener\CalculateTotalFieldSubscriber
arguments: ["@app.manager.day"]
app.form.day:
class: AppBundle\Form\Type\DayType
arguments: ["@app.subscriber.calculatetotal"]
tags:
- { name: form.type }
如您所见,我还添加了一个 Form EventListener,它监听 PRE_SUBMIT 和 POST_SET_DATA 以更改我的非映射 'total' 字段。
在DayType.php表格中:
private $calculateTotalFieldSubscriber;
public function __construct(CalculateTotalFieldSubscriber $calculateTotalFieldSubscriber)
{
$this->calculateTotalFieldSubscriber = $calculateTotalFieldSubscriber;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// I do not save this field to the database, so mapped is set to false
->add('total', TimeType::class, array(
'mapped' => false
))
$builder->addEventSubscriber($this->calculateTotalFieldSubscriber);
}
calculateTotalFieldSubscriber是默认的,监听两个事件。