想要使用Symfony访问Twig中的表单域

时间:2015-08-26 15:52:19

标签: symfony twig

我正在尝试从表单中获取orgid,以便将其传递给Ormember:测试控制器:my.html.twig中概述的操作使用:

{{ render(controller(
    'CompanyNameofBundle:OrgMember:test', {'orgid':1})) }}

现在有一个静态的" 1"但我想成为一个变量。

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

OrgController.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
 */
class Org
{
     /**
     * @var string
     */
    private $orgName;


     /**
     * @var integer
     */
    private $orgId;
    /** of course setters and getters for above */

}

1 个答案:

答案 0 :(得分:1)

因此获取实体是正常的,您唯一的问题是将其传递到模板中?然后我会说这是你的答案:

{{ render(controller( 'CompanyNameofBundle:OrgMember:test', {'orgid':entity.orgId})) }}