浏览代码

[FrameworkBundle] Adding two form-related methods to the base controller

With these two methods, the concept of a "form factory" doesn't need to be understood by a beginner to create forms.
Ryan Weaver 14 年之前
父节点
当前提交
9f4e88ec17
共有 1 个文件被更改,包括 28 次插入0 次删除
  1. 28 0
      src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

+ 28 - 0
src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

@@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Symfony\Component\Form\FormTypeInterface;
 
 /**
  * Controller is a simple implementation of a Controller.
@@ -107,6 +108,33 @@ class Controller extends ContainerAware
         return new NotFoundHttpException($message, $previous);
     }
 
+    /**
+     * Creates and returns a Form instance from the form type object
+     *
+     * @param FormTypeInterface $type   The built form type object
+     * @param mixed $data               The initial data for the form
+     * @param array $options            Options for the form
+     *
+     * @return Symfony\Component\Form\Form
+     */
+    public function createForm(FormTypeInterface $type, $data = null, array $options = array())
+    {
+        return $this->get('form.factory')->create($type, $data, $options);
+    }
+
+    /**
+     * Creates and returns a form builder instance
+     *
+     * @param mixed $data               The initial data for the form
+     * @param array $options            Options for the form
+     *
+     * @return Symfony\Component\Form\FormBuilder
+     */
+    public function createFormBuilder($data = null, array $options = array())
+    {
+        return $this->get('form.factory')->createBuilder('form', $data, $options);
+    }
+
     /**
      * Returns true if the service id is defined.
      *