Symfony2でform名を指定する方法
Symfony2でEntityに依存しない同一のformを複数発行したくて、form名を動的に変更する方法を調べてみたので忘れないうちのメモっとく。
結果的にはcreateNameBuilderを通してFormBuilderを発行してあげればいけた。
for ($i = 1;$i <= 3;$i++) { $builder = $this->get('form.factory')->createNamedBuilder('form', 'sampleForm' . $i); }
createNamedBuilderの引数はこんな感じ。
/** * Returns a form builder. * * @param string|FormTypeInterface $type The type of the form * @param string $name The name of the form * @param mixed $data The initial data * @param array $options The options * * @return FormBuilder The form builder * * @throws FormException if any given option is not applicable to the given type */ public function createNamedBuilder($type, $name, $data = null, array $options = array()) { // ... }