简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM