Skip to content

Commit

Permalink
Merge pull request #67 from norzechowicz/fix-sf-form-extension
Browse files Browse the repository at this point in the history
Fix sf form extension
  • Loading branch information
norberttech authored Jul 20, 2016
2 parents 51645fe + 4a18b12 commit ef74d2e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
],
"require": {
"php": ">=5.4.0",
"symfony/event-dispatcher" : "~2.2",
"symfony/property-access": "~2.3",
"symfony/options-resolver": "~2.6",
"symfony/event-dispatcher" : "^2.2|^3.0",
"symfony/property-access": "^2.3|^3.0",
"symfony/options-resolver": "^2.6|^3.0",
"fsi/reflection" : "0.9.*",
"fsi/data-indexer" : "0.9.*"
},
Expand All @@ -23,6 +23,8 @@
"doctrine/common": "~2.5",
"gedmo/doctrine-extensions": "~2.3.10",
"symfony/form" : "^2.4|^3.0",
"symfony/validator" : "^2.4|^3.0",
"symfony/translation": "^2.4|^3.0",
"symfony/doctrine-bridge": "^2.2|^3.0",
"symfony/security-csrf" : "^2.4|^3.0",
"symfony/routing" : "^2.2|^3.0",
Expand Down
25 changes: 12 additions & 13 deletions lib/Extension/Symfony/ColumnTypeExtension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public function bindData(ColumnTypeInterface $column, $data, $object, $index)
$form = $this->createForm($column, $index, $object);
$form->submit(array($index => $formData));
if ($form->isValid()) {
$data = $form->getData();
foreach ($data as $index => $fields) {
foreach ($form->getData() as $fields) {
foreach ($fields as $field => $value) {
$column->getDataMapper()->setData($field, $object, $value);
}
Expand Down Expand Up @@ -152,10 +151,10 @@ public function initOptions(ColumnTypeInterface $column)
*
* @param \FSi\Component\DataGrid\Column\ColumnTypeInterface $column
* @param mixed $index
* @param mixed $data
* @param mixed $object
* @return FormInterface
*/
private function createForm(ColumnTypeInterface $column, $index, $data)
private function createForm(ColumnTypeInterface $column, $index, $object)
{
$formId = implode(array($column->getName(),$column->getId(), $index));
if (array_key_exists($formId, $this->forms)) {
Expand Down Expand Up @@ -212,7 +211,7 @@ private function createForm(ColumnTypeInterface $column, $index, $data)
switch ($column->getId()) {
case 'datetime':
foreach ($fields as &$field) {
$value = $column->getDataMapper()->getData($field['name'], $data);
$value = $column->getDataMapper()->getData($field['name'], $object);
if (!isset($field['type'])) {
$field['type'] = $this->isSymfony3()
? $this->getDateTimeTypeName()
Expand Down Expand Up @@ -243,23 +242,23 @@ private function createForm(ColumnTypeInterface $column, $index, $data)
);
}

if (null !== $data) {
$formBuilderOptions[$this->isSymfony3() ? 'entry_options' : 'options'] = array(
'data_class' => ClassUtils::getRealClass(get_class($data))
);
}

if ($this->isSymfony3()) {
$formBuilderOptions['entry_options']['fields'] = $fields;
}


$formData = [];
foreach (array_keys($fields) as $fieldName) {
$formData[$fieldName] = $column->getDataMapper()->getData($fieldName, $object);
}


//Create form builder.
$formBuilder = $this->formFactory->createNamedBuilder(
$this->formName,
($this->isSymfony3())
? $this->getCollectionTypeName()
: 'collection',
array($index => $data),
array($index => $formData),
$formBuilderOptions
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use FSi\Component\DataGrid\Tests\Fixtures\EntityCategory;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use FSi\Component\DataGrid\Tests\Fixtures\Entity;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\ValidatorBuilder;

class FormExtensionTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -115,11 +118,13 @@ protected function setUp()
->method('getManagers')
->will($this->returnValue(array()));

$validatorBuilder = new ValidatorBuilder();
$resolvedTypeFactory = new ResolvedFormTypeFactory();
$formRegistry = new FormRegistry(array(
new CoreExtension(),
new DoctrineOrmExtension($managerRegistry),
new CsrfExtension(new CsrfTokenManager())
new CsrfExtension(new CsrfTokenManager()),
new ValidatorExtension($validatorBuilder->getValidator())
),
$resolvedTypeFactory
);
Expand All @@ -135,6 +140,39 @@ protected function setUp()
$this->extension->setDataGrid($dataGrid);
}

public function testAvoidBindingDataWhenFormIsNotValid()
{
$column = $this->createColumnMock();
$this->setColumnId($column, 'text');
$this->setColumnOptions($column, array(
'field_mapping' => array('name', 'author'),
'editable' => true,
'form_options' => array(
'author' => [
'constraints' => [
new Email()
]
]
),
'form_type' => array(
'name' => array('type' => $this->isSymfony3() ? 'Symfony\Component\Form\Extension\Core\Type\TextTyp' : 'text'),
'author' => array('type' => $this->isSymfony3() ? 'Symfony\Component\Form\Extension\Core\Type\TextTyp' : 'text'),
)
));

$object = new Entity('old_name');

$data = array(
'name' => 'object',
'author' => 'invalid_value',
);

$this->extension->bindData($column, $data, $object, 1);

$this->assertNull($object->getAuthor());
$this->assertSame('old_name', $object->getName());
}

public function testSimpleBindData()
{
$column = $this->createColumnMock();
Expand Down

0 comments on commit ef74d2e

Please sign in to comment.