jmunozco
jmunozco

Reputation: 695

How to pass value to twig in Symfony2

I'm trying to have inputs with customized labels (based on other entity attribute).

private $kilos_maxlim;
private $precio;

I set the values of $kilos_maxlim in the constructor of the class. So, I have that field filled. I want to show an input of $precio but with the label based on the value of $kilos_maxlim.

I have this input field in the classtype:

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

How can I pass the value without being an input?

Upvotes: 0

Views: 51

Answers (1)

insertusernamehere
insertusernamehere

Reputation: 23580

It should work as simple as this:

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(),
    ));
}

Upvotes: 2

Related Questions