2013-11-02 18 views
1

我来在我开发的应用程序中的常见问题,也有描述为三个用例:Symfony2:在处理之前预览表单数据的好模式是什么?

对象的创建形式必须遵循的一个预览页面,其中显示 对象在它的最终状态下,用一个按钮来完成创建,并返回一个按钮并使用表单对其进行修改。

我已经有了一个典型的基础创建流程。一个newAction,它使用窗体呈现newObject模板,该窗体将数据发送到检查数据的createAction,创建该对象并重定向到showObject操作。

我的主要想法是: 与以前一样,newAction使用将数据发送到createAction的窗体呈现newObject模板。这个createAction呈现一个previewObject模板,带有两个按钮,它们将表单发送到createAction angain,在那里我可以检查哪个按钮被按下,我可以决定再次显示对象数据的表单,或者完成cration流并重定向到showObject操作。

我的问题是:

  1. 有三种不同的按钮(预览,编辑和创建),这应视模板(新建,预览)来显示或hiddend。
  2. 除了按钮,previewObject模板中的窗体必须全部隐藏。 “预览”对象是否必须作为隐藏形式传递,还是有另一种我没有看到的方式?

我需要几种形式来实现此功能吗?我试图用一种形式和两种行为来考虑解决方案,但我不确定这是否足够。

这是一个常见的用例,但我一直无法在线找到示例。

任何帮助,将不胜感激。

回答

1

你的问题达到了我的好奇心,我尝试了一些不同的东西。想出了一个有趣的方式来处理它使用一个单一的形式和一个单一的行动在控制器中使用createFormBuilder,但我找不到一种方法使用createFormFormType,因为你不能在调用控制器后操纵表单$form = $this->createForm(new NewsletterSignupType());

这感觉有点冒失,但在这里,希望如果没有别的,它会给你一些想法。

class DemoController extends Controller 
{ 
    /** 
    * @Route("/", name="newsletter_signup") 
    * @Template() 
    */ 
    public function newsletterSignupAction(Request $request) 
    { 
     $form = $this->getNewsletterSignupForm(); 

     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $newButton = $form->get('new'); 
      $editButton = $form->get('edit'); 
      $createButton = $form->get('create'); 

      if ($newButton->isClicked()) { 
       $form = $this->getNewsletterSignupForm($form->getData(), true); 
      } 

      if ($editButton->isClicked()) { 
       /** 
       * We don't have to do anything here because we are just 
       * returning the original form with the submitted data attached 
       */ 
      } 

      if ($createButton->isClicked()) { 
       // User verified data and clicked "Create" 
       $response = 'Thank you for signing up!'; 

       return new Response($response); 
      } 
     } 

     return array(
      'form' => $form->createView() 
     ); 
    } 

    /** 
    * Handle creating the form and setting fields/buttons and data 
    */ 
    protected function getNewsletterSignupForm($data = null, $previewMode = false) 
    { 
     $actionUrl = $this->generateUrl('newsletter_signup'); 

     $formBuilder    = $this->createFormBuilder($data); 
     $fieldAttributes   = []; 
     $newButtonAttributes  = []; 
     $previewButtonAttributes = []; 

     if ($previewMode) { 
      $fieldAttributes['read_only'] = true; 
      $newButtonAttributes['attr'] = ['hidden' => true]; 
     } else { 
      $previewButtonAttributes['attr'] = ['hidden' => true]; 
     } 

     $formBuilder->add('name', 'text', $fieldAttributes) 
        ->add('email_address', 'email', $fieldAttributes) 
        ->add('new', 'submit', $newButtonAttributes) 
        ->add('edit', 'submit', $previewButtonAttributes) 
        ->add('create', 'submit', $previewButtonAttributes); 

     $formBuilder->setAction($actionUrl); 

     $form = $formBuilder->getForm(); 

     return $form; 
    } 
} 

查看代码:

{# Acme:DemoBundle:newsletterSignup.html.twig #} 

{% extends 'AcmeDemoBundle::layout.html.twig' %} 

{% block content %} 
    {{ form(form) }} 
{% endblock content %} 
+0

大,没有关于是哈克的问题,它就像一个魅力。也许有可能创建一个表单类,并在构造函数中传递参数,甚至作为服务注入它们,我也不是专家,但这是一个很好的开始,谢谢! – jmoreno

相关问题