2

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 Answer 1

3

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(),
        )));
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.