简体   繁体   English

Symfony:我应该为此表格使用FormBuilder吗?

[英]Symfony: should i use FormBuilder for this form?

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

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中的每个表单都应该使用FormBuilder构建。

Questions 问题

  • what is the best practice to create this form? 创建此表单的最佳实践是什么? if it's using FormBuilder is there any example i can see ? 如果使用FormBuilder,有没有我可以看到的示例? i couldn't find an example in Symfony documentation on how to do what im looking for 我在Symfony文档中找不到有关如何做即时消息的示例

  • if i should create my form without FormBuilder how can i implement CSRF protection ? 如果我应该在没有FormBuilder的情况下创建表单,该如何实现CSRF保护?

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. 您可以使用实体类型,并将Multiple选项设置为true,然后使您的实体列表带有复选框。

http://symfony.com/doc/current/reference/forms/types/entity.html http://symfony.com/doc/current/reference/forms/types/entity.html

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 aa request attribute or something: 对于不同的操作,您可以创建链接到控制器功能的按钮,只需确保使用javascript使按钮变成发布请求,然后将该操作分配为请求属性或其他内容即可:

eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM