想要使用 Symfony 访问 Twig 中的表单字段 [英] Want to access form fields in Twig using Symfony
本文介绍了想要使用 Symfony 访问 Twig 中的表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从表单中获取 orgid,以便我可以使用以下方法将其传递给 Ormember:test Controller:Action,如 my.html.twig 中所述:
I am trying to get the orgid from the form so I can pass it to the Ormember:test Controller:Action as outlined in my.html.twig using:
{{ render(controller(
'CompanyNameofBundle:OrgMember:test', {'orgid':1})) }}
现在哪里有一个静态的1",但我想成为一个变量.
Where for now there is a static "1" but I would like to be a variable.
my.html.twig
my.html.twig
{% extends 'CompanyNameofBundle::base.html.twig' %}
{% block body -%}
<h1>Organization Edit</h1>
{{ form(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}
<ul class="record_actions">
<li>
<a href="{{ path('org') }}">
Back to the list
</a>
</li>
<li>{{ form(delete_form) }}</li>
</ul>
{{ render(controller(
'CompanyNameofBundle:Search:shortjq')) }}
{{ render(controller(
'CompanyNameofBundle:OrgMember:test', {'orgid':1})) }}
{% endblock %}
组织控制器.php
/**
* Org controller.
*
* @Route("/org")
*/
class OrgController extends Controller
{
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CompanyNameofBundle:Org')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Org entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
}
Org.php(实体)
Org.php (Entity)
/**
* Org
*/
class Org
{
/**
* @var string
*/
private $orgName;
/**
* @var integer
*/
private $orgId;
/** of course setters and getters for above */
}
推荐答案
所以获取实体没问题,您唯一的问题是将其传递到模板中?那么我会说这是你的答案:
So getting the entity is OK and your only problem is passing it into the template? Then I would say this is your answer:
{{ 渲染(控制器('CompanyNameofBundle:OrgMember:test', {'orgid':entity.orgId})) }}
这篇关于想要使用 Symfony 访问 Twig 中的表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文