Symfony如何访问表单字段并传递给渲染表单?

时间:2015-09-02 16:44:36

标签: symfony twig

我有一个表单,里面有两个渲染的控制器(表单)。我想将一个变量从主窗体传递给渲染。我知道我可以这样做:

{{ render(controller(
'CompanyNameofBundle:Search:shortjq', {'orgid':entity.orgId})) }}

但是我在访问'orgid'这是主要形式。

my.html.twig

{% extends 'CompanyNameofBundle::base.html.twig' %}

{% block body -%}
    <h1>Organization Edit</h1>

    {{  form_start(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}

    <div class="form-group'">
        <div class="col-md-2">{{ form_label(edit_form.orgName, 'Organization Name') }}</div>
        <div class="col-md-4">{{ form_widget(edit_form.orgName) }}</div>


        <div class="hidden">{{ form_widget(edit_form.orgId) }}</div>


        <div>&nbsp;</div><div>&nbsp;</div>
    </div>
<ul class="record_actions">
    <li>{{ form_end(edit_form) }}</li>
    <li>{{ form(delete_form) }}</li>
    <li>
        <a href="{{ path('org') }}">
            Back to the list
        </a>
    </li>
</ul>

    {{ render(controller(
    'CompanyNameofBundle:Search:shortjq', {'orgid':entity.orgId})) }}


{% if entity.orgId is not null %}
    {{ render(controller(
        'CompanyNameofBundle:OrgMember:test', {'orgid':entity.orgId})) }}
{% endif %}


{% endblock %}

SearchController.php

/**
     * @Route("/shortjq", name="shortjq")
     * @Template()
     */
    public function shortjqAction()
    {
        $form   = $this->createForm(new JqueryType(), null, [
            'action' => '',
            'method' => 'POST'
        ]);
        return array(
            'form'   => $form->createView(),
        );
    }

JqueryType.php

/**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('POST')
            ->add('name', 'text')
            ->add('orgid', 'integer')
            ->add('search', 'submit')
        ;

    }
    /**
     * @return string
     */
    public function getName()
    {
        return 'companynameofbundle_jquery';
    }

1 个答案:

答案 0 :(得分:0)

要将表单小部件访问到我使用的视图中:

{{ form_widget(form.orgid) }}

如果您还想为该窗口小部件添加属性,可以像这样添加属性(例如,向窗口小部件添加类):

{{ form_widget(form.org, { 'attr': {'class': 'the-css-class-you-want'} }) }}

您可以在此处搜索更多信息Twig Template Form Function and Variable Reference