如何以相同的形式显示另一个实体的多个信息?

时间:2012-06-01 12:55:01

标签: forms symfony

实际上,我尝试在中间实体上创建一个表单,我想为同一个实体显示许多输入(项目颜色和项目名称)。我也可以创建项目实体。

# MyNiceBundle/Form/Type/CollaborationType.php

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('project', 'entity', array(
        'class' => 'MyNiceBundle:Project',
        'property' => 'name'
    ));
    $builder->add('project', 'entity', array(
        'class' => 'MyNiceBundle:Project',
        'property' => 'color'
    ));
    $builder->add('participant', 'entity', array(
        'class' => 'MyNiceBundle:User',
        'property' => 'email',
        'multiple' => true,
        'expanded' => true
    ));
    $builder->add('workgroup', 'entity', array(
        'class' => 'MyNiceBundle:Workgroup',
        'property' => 'name',
        'multiple' => false,
        'expanded' => true
    ));
}


# Resources/Views/project_form.html.twig

<form id="project-creation" class="form" action="{{ path('nicebundle_project_create') }}" method="post">
    {{ form_errors(form) }}
    <div class="project">
    # Here I want to show the input for project name
    {{ form_errors(form.project) }}
    {{ form_widget(form.project) }}
    </div>

    <div id="navigation-panel">
        # Here I want to show the input for project color
        {{ form_errors(form.project) }}
        {{ form_widget(form.project) }}

        {{ form_errors(form.participant) }}
        {{ form_widget(form.workgroup) }}
        {{ form_widget(form._token) }}
        <input class="btn" type="reset" value="reset" />
        <input class="btn" type="submit" value="submit" />
    </div>
</form>

我该怎么办?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我在CollaborationType中传递了ProjectType,如下所示:

# MyNiceBundle/Form/Type/CollaborationType.php
public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('project', new ProjectType());
    $builder->add('participant', 'entity', array(
        'class' => 'BtaskUserBundle:User',
        'property' => 'email',
        'multiple' => false,
        'expanded' => true
    ));
    $builder->add('workgroup', 'entity', array(
        'class' => 'BtaskBoardBundle:Workgroup',
        'property' => 'name',
        'multiple' => false,
        'expanded' => true
    ));
}

# Resources/Views/project_form.html.twig
<form id="workgroup-form-create" class="form" action="{{ path('MyNiceBundle_project_create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_errors(form) }}
    <div class="project">
    {{ form_errors(form.project) }}
    {{ form_widget(form.project) }}
    </div>

    <div id="navigation-panel">
        {{ form_errors(form.project) }}
        {{ form_widget(form.project) }}

        {{ form_errors(form.participant) }}
        {{ form_widget(form.participant) }}

        {{ form_errors(form.workgroup) }}
        {{ form_widget(form.workgroup) }}

        {{ form_widget(form._token) }}
        <input class="btn" type="reset" value="reset" />
        <input class="btn" type="submit" value="submit" />
    </div>
</form>