G Cham
G Cham

Reputation: 29

How to programatically display text in a symfony page without an input field

I want to display different messages to a page built by Symfony depending on the situation. I initially set it up by creating a hidden field and setting the label to whatever message I want:

$builder->add('pageTopMsg', 'hidden', array(
                    'label'     => $this->getPageTopMsg(),
                    'required'  => false,
                ))

That works, but it doesn't feel right. Plus Symfony says I have to create getters and setters in an entity. The messages and the hidden field don't have any relationship with an entity. Is there a better way to display messages on a form dynamically.

Upvotes: 0

Views: 645

Answers (2)

progg
progg

Reputation: 294

To display a form field's value with twig:

{{ form.vars.value.pageTopMsg }}

"form" is the name of your form, and pageTopMsg is your field

But if you just want to show a text not related to you entity, you can pass it from the Controller and show it with {{ pageTopMsg }}

Upvotes: 1

juanccq
juanccq

Reputation: 21

If the field does not have any relation with an entity, you can add this field directly in your Twig form and pass values through out your controller

$this -> render('entity/edit.hmtl.twig', array('someParameters' => array('val1', 'val2')));

Upvotes: 0

Related Questions