Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:20:31+00:00 2026-06-08T18:20:31+00:00

I have a FormType in Symfony2. It is used to display the settings. The

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:20:32+00:00Added an answer on June 8, 2026 at 6:20 pm

    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(),
            )));
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have want to save a form in Symfony2, based on a form type,
I have a PostType form that extends AbstractType. In controller I'd like to add
I have a user edit form where I would like to administer the roles
I have radioboxes <form method=post id=myform> <label>A</label> <input type=radio name=formtype value = 1 checked=checked>
I have a batch class (KC8) which is using page/form recognition and separator zones
I am using jquery validation 1.9.0. I have used the latest Jquery and on
I have added a second submit button to my form, now Symfony2 complains about
Is possible create (register) a new class in runtime using delphi. I have a
I have a search form I generated using the filterGrid option in JqGrid. I
I have a custom form type for a Rating entity in my Symfony2 project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.