Sonata Admin: modify validation groups in an admin extension

SonataAdminBundle has a powerful extensions feature which allows you to add or change features of one or more Admin instances.

I needed my extension to change some validation rules, and it turned out that imposing different validation groups was a good way to achieve it. 

$formOptions attribute of Sonata\AdminBundle\Admin\AbstractAdmin class allows for setting validation groups, but I couldn’t find an easy way to modify it in my extension.

getFormBuilder method of AbstractAdmin class is where these $formOptions are added to the form builder:

So, in my admin class I override this method, to add support for extensions:

Now, in your admin extension you can add a method to actually modify $formOptions of the admin class:

By Aleksandar Jakovljević

My name is Aleksandar Jakovljević and I am a web developer from Belgrade, Serbia. I was born in Smederevo, in 1980. I started focusing on web development since late 2003. I am mostly focused on open source web technologies. I specialised in PHP, focusing on Symfony framework, with skills in Drupal and WordPress, too. I worked more than 7 years for art_net studio and 10 years for Computer Rock (former Spoiled Milk). Now I am working on developing industry leading subscription management system as a part of Plenigo team since August 2021. I love the web and everything related to it. The possibility to publish your ideas quickly and make them available to the entire world is what made me decide to pursue a career in web development.

Follow on: Twitter | GitHub | LinkedIn | Facebook | Instagram

Hire me on Upwork

Leave a comment

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.