简体   繁体   中英

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.

Questions

  • 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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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