表单视图中的多对多关系 symfony 2 [英] many to many relation in a form view symfony 2

查看:31
本文介绍了表单视图中的多对多关系 symfony 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体之间的多对多关系.

I have a many to many relation between two entities.

然后我显示一个表单以将 entityA 添加到 entityB.是否无法添加自定义表单(我的意思是在树枝视图中)以使用户有时可以选择一个值,有时可以选择多个值?

I display then a form to add entityA to entityB. Isn't possible to add a customized form (I mean in the twig view) in order to enable the user sometimes to select one value and sometimes more than one?

当我希望用户选择多个值时,我使用这个

when I want the user to select more than one value, I use this

<select multiple>
{% for entity in entitys %}
<option> 
{{entity.id}}

 </option>
{%endfor%}
</select>

否则这个

 <select >
    {% for entity in entitys %}
    <option> 
    {{entity.id}}

     </option>
    {%endfor%}
    </select>

但现在的问题是如何提交表单.

but now the problem is how to submit the form. the

<button  type="submit"  class="btn btn-info"    value="NEXT STEP " /> 

这是整个表格

<form method="post">
  <select >
    {% for entity in entitys %}
    <option> 
    {{entity.id}}

     </option>
    {%endfor%}
    </select>

<input  type="submit"     /> 

</form>

不再提交表单.有什么想法吗??

no longer submit the form. any ideas plz??

这是我的整个树枝视图

  <h2> STEP {{step}} </h2>
  <form method="post">
<select >
{% for entity in entitys %}
<option value="{{entity.id}}"> 
{{entity.id}}

 </option>
{%endfor%}
</select>

<input  type="submit"  class="btn btn-info"     /> 

</form>
  <br>
  <br>

推荐答案

在您的表单构建器中,您可以添加一些选项,例如我的示例:

In your formbuilder you can add some options like in my exemple:

目的是将您的字段映射到实体(以设置列表).不要忘记在你的映射实体中添加一个 _tostring 方法,让 symfony 能够在你的选择中将你的实体表示为文本.

The aim is to map your field to an entity (to setup the list). Don't forget to add a method _tostring to your mapped entity to make symfony able to represent your entity as a text in your select.

public function buildForm(FormBuilder $builder, array $options) {
        $id = $this->id;

        $builder->add(
            'addressees',
            'entity',
            array(
                        'class' => 'Pref27\MailBundle\Entity\Addressee',
                        'property' => 'name',
                        'multiple' => true,
                    'expanded' => false,
                        'required' => true,
                        'label' => 'mail.add.theme';
                }
            )
        );
    }

在你的表单控制器中

$editForm = $this->createForm(new FormType(), $entity);
return array(
            'form'   => $editForm->createView()
        );

在你看来

<form action="{{ path('theControllerActionWitchIsResponsibeOfRecordingIntoDatabase' }}" method="post" {{ form_enctype(edit_form) }}>
        {{ form_widget(edit_form) }}
        <p>
            <button type="submit">Next step</button>
        </p>
</form>

渲染的字段类型将取决于multiple和expended的设置

The type of field rendered will depend on setting of multiple and expended

select tag                                  expanded=false  multiple=false
select tag (with multiple attribute)        expanded=false  multiple=true
radio buttons                               expanded=true   multiple=false
checkboxes                                  expanded=true   multiple=true

您可以在此处找到有关表单中实体类型的更多信息:http://symfony.com/doc/2.0/reference/forms/types/entity.html

you can find more information about entity type in form here : http://symfony.com/doc/2.0/reference/forms/types/entity.html

从您的树枝视图中缺少操作尝试添加

From your twig view form's action is missing try to add

<form method="post" action="{{ path("theRouteOfYourControllerWitchRecordTheData")}}">

不要忘记添加 {{form_rest(form) }} 告诉 twig 添加 CSRF 令牌

don't forget to add {{form_rest(form) }} To tell twig to add the CSRF token

不要忘记在选择的选项中添加价值

and don't forget to add value in your select's option

<select multiple>
  {% for entity in entitys %}
       <option value="{{entity.id}}">{{entity.name}}</option>
  {%endfor%}
</select>

这篇关于表单视图中的多对多关系 symfony 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆