Last active
January 5, 2017 11:28
Revisions
-
rosstuck revised this gist
Apr 10, 2014 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,7 @@ class FormSet public function __construct() { // TODO: Maybe a[$form->getName() is better] What if two forms with the same name, would fail isSubmitted check... $this->forms = new SplObjectStorage(); } @@ -40,4 +41,14 @@ protected function getActiveForm(Request $request) } } } public function createViews() { $views = []; foreach ($this->forms as $form) { $views[] = $form->createView(); } return $views; } } -
rosstuck revised this gist
Apr 10, 2014 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
rosstuck created this gist
Apr 10, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ <?php use \Symfony\Component\Form\FormInterface; use \Symfony\Component\HttpFoundation\Request; class FormSet { /** * @var SplObjectStorage|FormInterface[] */ protected $forms; public function __construct() { $this->forms = new SplObjectStorage(); } public function addForm(FormInterface $form, callable $onComplete) { $this->forms[$form] = ['form' => $form, 'onComplete' => $onComplete]; } public function submitRequest(Request $request) { $form = $this->getActiveForm($request); if (!$form || !$form->isValid()) { return; } // Invoke completed callback $this->forms[$form]['onComplete']($form); } protected function getActiveForm(Request $request) { foreach ($this->forms as $form) { $form->handleRequest($request); if ($form->isSubmitted()) { return $form; } } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <?php $formset = new FormSet(); $formset->addForm( $this->createForm('form_1', $foo), function($form) use ($commandBus) { $commandBus->execute($form->getData()); } ); $formset->addForm( $this->createForm('form_1', $bar), function() { echo 'form 2 complete, yay!'; } ); $formset->submitRequest(new Request());