Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged branch marcw/issue-1746 (PR #1758)
Commits ------- 2e024f8 [Form] CollectionType now checks for data_class parameter instead of only class. 0327beb [Form] Added ObjectFactoryListener. Fixes #1746. Discussion ---------- [Form] Added ObjectFactoryListener. Fixes #1746. --------------------------------------------------------------------------- by marcw at 2011/07/21 09:32:17 -0700 This patch also fixes a validation issue because it was impossible for the validator to validate an array. --------------------------------------------------------------------------- by stof at 2011/07/21 09:47:46 -0700 yeah, using the data_class of the prototype would be great
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/Symfony/Component/Form/Extension/Core/EventListener/ObjectFactoryListener.php
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Core\EventListener; | ||
|
||
use Symfony\Component\Form\Event\FilterDataEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\Form\Util\PropertyPath; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Based on scalar values, this class instanciates object and hydrate them. | ||
* | ||
* @author Marc Weistroff <marc.weistroff@sensio.com> | ||
*/ | ||
class ObjectFactoryListener implements EventSubscriberInterface | ||
{ | ||
private $class; | ||
|
||
/** | ||
* __construct | ||
* | ||
* @param string $class FQCN class | ||
*/ | ||
public function __construct($class) | ||
{ | ||
$this->class = $class; | ||
} | ||
|
||
static public function getSubscribedEvents() | ||
{ | ||
return array( | ||
FormEvents::BIND_NORM_DATA => 'onBindNormData', | ||
); | ||
} | ||
|
||
public function onBindNormData(FilterDataEvent $event) | ||
{ | ||
$data = $event->getData(); | ||
|
||
foreach ($data as $k => $array) { | ||
if (!is_array($array)) { | ||
continue; | ||
} | ||
|
||
$object = new $this->class(); | ||
foreach ($array as $key => $value) { | ||
$path = new PropertyPath($key); | ||
$path->setValue($object, $value); | ||
} | ||
|
||
$data[$k] = $object; | ||
} | ||
|
||
$event->setData($data); | ||
} | ||
} | ||
|
This file contains 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 characters
This file contains 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 characters
24 changes: 24 additions & 0 deletions
24
tests/Symfony/Tests/Component/Form/Fixtures/AuthorType.php
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Symfony\Tests\Component\Form\Fixtures; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
class AuthorType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
$builder | ||
->add('last_name', 'text') | ||
; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'author'; | ||
} | ||
} | ||
|