0

我是 Symfony2 / twig 的新手,我需要使用预先保存的值渲染一个表单。

我可以让表单加载/保存数据,但我不能让它显示预先保存的值。我错过了什么?

这是我的控制器代码:

/**
 * @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));
}

这是我的树枝/表单代码:

<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>
4

2 回答 2

1

您需要将表单绑定到请求,然后将使用用户输入的数据填充表单。我已经更新了你的代码。

$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));

您还需要确保当用户提交表单时,它会转到输出表单的操作。我现在怀疑您有单独的操作来处理表单。

于 2013-08-08T15:19:52.510 回答
0
  1. 创建自定义 FormType http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html

  2. 用您想要的数据填充您的实体并将其传递给表单。(来自数据库/静态/api/等)

  3. 然后按照关于如何从实体获取数据到表单的现有答案,以便在显示表单时可以创建现有值:如何将实体属性值传递给表单 Symfony2?当然,您可以将其设为默认选择,而不是属性类型。

于 2014-05-20T14:24:05.917 回答