Symfony2, how to access Entity values inside Form?

人走茶凉 提交于 2019-12-24 02:18:50

问题


I have a FormType in Symfony2. It is used to display the settings. The settings are stored as entities in a database. Using Doctrine2, I fetch the settings and create a form, like below:

public function showSettingsAction()
{
    if(false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
        throw new AccessDeniedException();
    }
    $settings = new CommunitySettings();

    $repository = $this->getDoctrine()->getRepository('TestTestingBundle:CommunitySettings');
    $allSettings = $repository->findAll();

    $form = $this->createForm('collection', $allSettings, array(
        'type' => 'settings_form'
    ));
    $request = $this->container->get('request');
    if($request->getMethod() === 'POST') {
        $form->bindRequest($request);
        if($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $settings = $form->getData();
            foreach($settings as $setting) {
                $oldsetting = $em->getRepository('TestTestingBundle:CommunitySettings')
                    ->find($setting->getId());
                if(!$oldsetting) {
                    throw $this->createNotFoundException('No setting found for id '.$setting->getId());
                }

                $oldsetting->setSettingValue($setting->getSettingValue());
                $em->flush();
            }

            $this->get('session')->setFlash('message', 'Your changes were saved');

            return new RedirectResponse($this->generateUrl('_admin_settings'));
        }
    }


    return $this->render('TestTestingBundle:Admin:settings.html.twig',array(
        'form' => $form->createView(),
    ));
}

This is the line of code where I send the array of $allSettings to the settings_form:

$form = $this->createForm('collection', $allSettings, array(
    'type' => 'settings_form'
));

This is how the settings form looks like:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('settingValue', 'text');
}

I have a label, a value and a field type stored in the entity and I would like to use those for building the form. However, when I use this it only shows me the variable names in the Form, like this:

0
Settingvalue //Is a checkbox, where it says Settingvalue, it should be the label stored in the entity
0

1
Settingvalue //Is a integer, where it says Settingvalue, it should be the label stored in the entity
3000

How can I use the variables stored in the Entity to build the Form fields with?


回答1:


You can use an event listener in your settings form type to solve this problem.

public function buildForm(FormBuilder $builder, array $options)
{
    $formFactory = $builder->getFormFactory();
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
        $form = $event->getForm();
        $data = $event->getData();

        $form->add($formFactory->createNamed('settingsValue', $data->getSettingsType(), array(
            'label' => $data->getSettingsLabel(),
        )));
    });
}


来源:https://stackoverflow.com/questions/11357748/symfony2-how-to-access-entity-values-inside-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!