0
votes

Im new to Symfony2 / twig and I need to render a form with pre-saved values.

I can get the form to load / save data but I cant get it to show the pre-saved values. What am I missing?

Here is my controller code:

/**
 * @Route("/build/{id}")
 * @Template()
 */
public function buildAction($id)
{
    $request = $this->getRequest();
    $em = $this->getDoctrine()->getEntityManager();
    $repo = $em->getRepository('ImagineNewsletterBundle:Section');
    $section = $repo->find($id);


    $newsletter = $section->getNewsletter();
    $globalMag = $newsletter->getMagazine();

    //$globalMag = unserialize($globalMag[0]);


    $builder = $this->get('newsletter.builders')->getBuilder($section->getBuilder());
    $builder->setSearchUrl($this->generateUrl('imagine_newsletter_section_search', array('builder' => $section->getBuilder())));

    $form = $this->createForm($builder->getSearchFormBuilder());    

    $prevArticles = $section->getArticles();      

   // $form->bind($prevArticles);


    return $this->render('ImagineNewsletterBundle:Section:'.$builder->getTemplate() , array('prevArticles' => $prevArticles,'builder' => $builder, 'form' => $form->createView(), 'section' => $section, 'newsletter' => $newsletter,  'globalmag' => $globalMag));
}

Here my twig/form code:

<form id="advert" action="{{ addAticle }}" method="post" {{ form_enctype(form) }} class="form-stacked">

                    <div class="form_element">
                        {{ form_label(form.topLogoAdvert) }}
                        {{ form_errors(form.topLogoAdvert) }}
                        {{ form_widget(form.topLogoAdvert) }}
                    </div>

                    <div class="form_element">
                        {{ form_label(form.topLogoAlt) }}
                        {{ form_errors(form.topLogoAlt) }}
                        {{ form_widget(form.topLogoAlt) }}
                    </div>


                    <div class="form_element">
                        {{ form_label(form.topLogoLink) }}
                        {{ form_errors(form.topLogoLink) }}
                        {{ form_widget(form.topLogoLink) }}
                    </div>

                    <input type="hidden" name="section" value="{{ section.id }}" />

                        {{ form_widget(form) }}

                        <div class="well">
                            <button type="submit" class="btn primary">Save</button>
                            <button id="reset" type="reset" class="btn">Reset</button>
                        </div>
                        {{ form_rest(form) }}
                </form>
2
It doesn't appear our binding an entity to the form.Moylin
Can you provide the full action from your controller.Gavin Love
Sure...see the update controller above.Bob Flemming

2 Answers

1
votes

You need to bind the form to the request, that will then populate the form with the data the user entered. I have updated your code.

$request = $this->getRequest();
$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository('ImagineNewsletterBundle:Section');
$section = $repo->find($id);

$builder = $this->get('newsletter.builders')->getBuilder($section->getBuilder());
$builder->setSearchUrl($this->generateUrl('imagine_newsletter_section_search', array('builder' => $section->getBuilder())));

$form = $this->createForm($builder->getSearchFormBuilder(), $section);

if ($request->isMethod('post')) {
    $form->bind($request);
}

return $this->render('ImagineNewsletterBundle:Section:'.$builder->getTemplate() , array('builder' => $builder, 'form' => $form->createView(), 'section' => $section));

You also need to ensure that when the form is submitted by the user that it goes to the action that output the form. I suspect now that you have a separate action to process the form.

0
votes
  1. Create a custom FormType http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html

  2. Fill your entity with the data you want and pass it to the form. (From database/staticly/api/etc.)

  3. Then follow this existing answer on how to get data from the entity to the form so that when you display the form you can create existing values: How to pass entity atribute value to form Symfony2? Of course instead of the attribute type you make it a default choice.