Override CRUDController to add custom form elements to list template for batch action usage in SonataAdminBundle

In my use case I needed a batch action with an additional date field to be set on my Entity. I came up with the following solution.

I will not add the Entities since this post should focus on the configuration of the list template and batch action.


First of all define the custom batch action and the path to your custom list template in your Admin class:


Next enable and configure a custom CRUDController:


Then create your custom CRUDController:


Copy the original listAction from CRUDController.php and add the custom form date element:

We can use the sonata_type_date_picker which comes built-in with the SonataAdminBundle.

Next add the date picker to your template:

This should render some HTML and Javascript for the element looking like this:


In our Admin class we set ask_confirmation to true. This way we can make use of the relevancy check function to validate our new form element.

Regarding the naming conventions add a batchActionTestIsRelevant method to your CRUDController:


Finally we add the batchActionTest method:

And we’re done!