Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active February 14, 2023 14:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/fef9e296e77bb879d138 to your computer and use it in GitHub Desktop.
Save webdevilopers/fef9e296e77bb879d138 to your computer and use it in GitHub Desktop.
How to use form event listener in Sonata Admin
<?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);
});
}
}
<?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);
});
}
}
}
}
@webdevilopers
Copy link
Author

Related issues regarding validation by @ethernal :

@webdevilopers
Copy link
Author

@karousn
Copy link

karousn commented Jun 8, 2016

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