Last active
February 14, 2023 14:07
-
-
Save webdevilopers/fef9e296e77bb879d138 to your computer and use it in GitHub Desktop.
How to use form event listener in Sonata Admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sonata\AdminBundle\Admin\Admin; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
class SubjectOutsideFunctionAdmin extends Admin | |
{ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$admin = $this; | |
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, | |
function (FormEvent $event) use ($formMapper, $admin) { | |
$form = $event->getForm(); | |
$subject = $admin->getSubject($event->getData()); | |
if ($form->has('durationType')) { | |
$form->remove('durationType'); | |
} | |
$disabled = $subject->getAbsenceType() === null ? true : false; | |
$durationType = $formMapper->getFormBuilder()->getFormFactory()->createNamed('durationType', 'sonata_type_translatable_choice', null, array( | |
'choices' => DurationType::getTypeList(), | |
'catalogue' => 'TimekeepingBundle', | |
'expanded' => true, | |
'required' => false, | |
'empty_value' => 'type_not_specified', | |
'attr' => array('disabled' => $disabled), | |
'disabled' => $disabled, | |
'auto_initialize' => false // Automatic initialization is only supported on root forms. | |
)); | |
$form->add($durationType); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sonata\AdminBundle\Admin\Admin; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
class SubjectOutsideFunctionAdmin extends Admin | |
{ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$subject = $this->getSubject(); | |
if ($subject->getId() !== null) { | |
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, | |
function (FormEvent $event) use ($formMapper, $subject) { | |
$form = $event->getForm(); | |
if ($form->has('durationType')) { | |
$form->remove('durationType'); | |
} | |
$disabled = $subject->getAbsenceType() === null ? true : false; | |
$durationType = $formMapper->getFormBuilder()->getFormFactory()->createNamed('durationType', 'sonata_type_translatable_choice', null, array( | |
'choices' => DurationType::getTypeList(), | |
'catalogue' => 'TimekeepingBundle', | |
'expanded' => true, | |
'required' => false, | |
'empty_value' => 'type_not_specified', | |
'attr' => array('disabled' => $disabled), | |
'disabled' => $disabled, | |
'auto_initialize' => false // Automatic initialization is only supported on root forms. | |
)); | |
$form->add($durationType); | |
}); | |
} | |
} | |
} | |
} |
i have this error : Please provide a valid sonata_field_description
option
related issue : sonata-project/SonataAdminBundle#3072
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issues related to data setting: