简体   繁体   中英

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.

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 }}

如果该字段与实体没有任何关系,则可以直接在Twig表单中添加该字段,然后将值传递给控制器

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

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