如何在Symfony2中将值传递给twig

时间:2016-08-09 11:43:10

标签: forms symfony

我正在尝试使用自定义标签(基于其他实体属性)输入。

private $kilos_maxlim;
private $precio;

我在类的构造函数中设置$kilos_maxlim的值。所以,我填补了这个领域。我想显示$precio的输入,但标签基于$kilos_maxlim的值。

我在classtype中有这个输入字段:

->add('precio', null, array(
    'attr' => array('autofocus' => true),
    'label' => 'label.precio',
))

如何在不作输入的情况下传递值?

1 个答案:

答案 0 :(得分:2)

它应该像这样简单:

public function buildForm(FormBuilderInterface $builder, array $options) {
    // get the actual entity
    $entity = $builder->getData();

    // set the value as the label
    $builder->add('precio', null, array(
        'label' => 'label.precio ' . $entity->getKilosMaxlim(),
    ));
}
相关问题