简体   繁体   中英

Dynamically change content of sonata_type_model field

I use Sonata admin bundle and type sonata_type_model for some field on page.

How I can dynamically change list of elements in this field? I need change content of this field depending on the value in another field.

Eg, in field with type sonata_type_model are showed all categories of something. And in another field on page I can select gender (M or F). How I could automatically change content of field with categories if value of gender was selected? In this example for field with categories applying type sonata_type_model is important because this type allow select existing categories and enter new category.

You should use form events to handle the dynamic generation of your form

Dynamic Generation for Submitted Forms

Sample

Function configureFormFields in Admin class

$formBuilder = $formMapper->getFormBuilder();
        $currentUser = $this->getConfigurationPool()->getContainer()->get('security.token_storage');
$formMapper->add('driverCompany',null,array('class' =>'XXXXXBundle:Company'));
                $formModifier = function (FormInterface $form, Company $oCompany = null, $defaultCompany) {
                    $company = null === $oCompany ? $defaultCompany : $oCompany;
                    $form->add(
                        'zones',
                        null,
                        array(
                            'class' => 'XXXXXXBundle:Zone',
                            'label' => 'Zones',
                            'multiple' => true,
                            'required' => false,
                            'placeholder' => '',
                            'query_builder' => function (ZoneRepository $zr) use ($company) {
                                return $zr
                                    ->createQueryBuilder('z')
                                    ->where('z.companyZone = ?1')
                                    ->setParameter(1, $company);
                            },
                        )
                    );
                };
                $formBuilder->addEventListener(
                    FormEvents::PRE_SET_DATA,
                    function (FormEvent $event) use ($formModifier, $defaultCompany) {
                        $data = $event->getData();

                        if ($data !== null) {
                            if ($data->getDriverCompany() === null) {
                                $oCompany = null;
                            } else {
                                $oCompany = $data->getDriverCompany();
                            }
                            $formModifier($event->getForm(), $oCompany, $defaultCompany);
                        }
                    }
                );

                $formBuilder->get('driverCompany')->addEventListener(
                    FormEvents::POST_SUBMIT,
                    function (FormEvent $event) use ($formModifier, $defaultCompany) {

                        $oCompany = $event->getForm()->getData();
                        $formModifier($event->getForm()->getParent(), $oCompany, $defaultCompany);
                    }
                );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM