symfonysymfony-formssonata-adminsonatasymfony-sonata

sonataadmin - add form field at the begining of the ertain existing tab only if condition is true


I have $form and some fields in the different tabs, that works okay, I want to add a field into the form

$this->getSubject()->getId() 

if this condition is not NULL

What I did, after defining form that should be visible in all conditions i added this part of code, to add field url in the tab dimension (tab dimension is already added in previous formmapper definition):

if($this->getSubject()->getId()  !== NULL){
$formMapper
        ->with('tab.dimension')
                ->add(
                'url',
                null,
                [
                    'required' => false,
                ]
            )
->end();
}

But the error i recive is:

New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.

Any help?


Solution

  • Use tab like this. If you want to add grouping than use it with function.

    if($this->getSubject()->getId() !== NULL) {
                $formMapper
                    ->tab('dimension')
                        ->add('url', null, [
                            'required' => false,
                        ])
                    ->end();
            }