Symfony:我应该为此表格使用FormBuilder吗? [英] Symfony: should i use FormBuilder for this form?

查看:81
本文介绍了Symfony:我应该为此表格使用FormBuilder吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到有关如何创建此表单的最佳实践,我有一个实体,我想创建一个表单,该表单将列出所有实体以及每个复选框的复选框,并且用户可以使用复选框选择多个实体并对所有选定的实体应用操作,如下所示:

I'm trying to find the best practice on how to create this form, i have an entity and i want to create a form that will list all entities along with a checkbox for each and the user can select several entities using checkbox and apply an action to all the selected entities, something like this:

-Actions
--- Remove
--- Disable
--- Enable

#  , Title     , Desc
[] , title1    , This is a desc one
[] , Title2    , this is a desc two
[] , Title3    , this is a desc three

//[] is a checkbox

在不使用FormBuilder的情况下创建此表单非常简单,但是我可以看到symfony中的每个表单都应该使用FormBuilder构建.

it's pretty straightforward to create this form without using the FormBuilder but as i can see every form in symfony should be a built using FormBuilder.

问题

  • 创建此表单的最佳做法是什么?如果它使用FormBuilder,有没有我可以看到的示例?我无法在Symfony文档中找到有关如何做即时消息的示例

  • what is the best practice to create this form? if it's using FormBuilder is there any example i can see ? i couldn't find an example in Symfony documentation on how to do what im looking for

如果我应该在没有FormBuilder的情况下创建表单,该如何实现CSRF保护?

if i should create my form without FormBuilder how can i implement CSRF protection ?

推荐答案

表单构建器非常适合此操作.您可以使用实体类型,并将multiple选项设置为true,然后使您的实体列表带有复选框.

Form builder is perfect for this. you can use the entity type and set the multiple option equal to true and you'll have your list of entities complete with checkboxes.

http://symfony.com/doc/current/reference/形式/类型/entity.html

对于不同的操作,您可以创建链接到控制器功能的按钮,只需确保使用javascript使按钮变成发布请求,然后将该操作分配为请求属性或其他内容即可.

for the different actions you can create buttons that link to the controller function, just make sure you use javascript to make the buttons turn into post requests and assign the action as a a request attribute or something:

例如

<a href="{{ path('path_to_form_controller') }}" class="some_class">Remove</a>

然后在您的控制器中可以检查操作

and then in your controller you can check the action

switch ($action) {
    case 'remove':
        //do something
    break;
    default:
}

这篇关于Symfony:我应该为此表格使用FormBuilder吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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