Skip to content

Commit

Permalink
Added Symfony3 form support
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Jan 24, 2016
1 parent 39ca2f6 commit 9c4ec65
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
69 changes: 60 additions & 9 deletions lib/Extension/Symfony/ColumnTypeExtension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private function createForm(ColumnTypeInterface $column, $index, $data)
case 'entity':
$field = array(
'name' => $column->getOption('relation_field'),
'type' => 'entity',
'type' => $this->isSymfony3() ? $this->getEntityTypeName() : 'entity',
'options' => array(),
);

Expand Down Expand Up @@ -214,7 +214,9 @@ private function createForm(ColumnTypeInterface $column, $index, $data)
foreach ($fields as &$field) {
$value = $column->getDataMapper()->getData($field['name'], $data);
if (!isset($field['type'])) {
$field['type'] = 'datetime';
$field['type'] = $this->isSymfony3()
? $this->getDateTimeTypeName()
: 'datetime';
}
if (is_numeric($value) && !isset($field['options']['input'])) {
$field['options']['input'] = 'timestamp';
Expand All @@ -229,22 +231,34 @@ private function createForm(ColumnTypeInterface $column, $index, $data)
break;
}

$formBuilderOptions = array(
'type' => new RowType($fields),
'csrf_protection' => false,

);
if ($this->isSymfony3()) {
$formBuilderOptions = array(
'entry_type' => $this->getRowTypeName(),
'csrf_protection' => false,
);
} else {
$formBuilderOptions = array(
'type' => new RowType($fields),
'csrf_protection' => false,
);
}

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

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

//Create form builder.
$formBuilder = $this->formFactory->createNamedBuilder(
$this->formName,
'collection',
($this->isSymfony3())
? $this->getCollectionTypeName()
: 'collection',
array($index => $data),
$formBuilderOptions
);
Expand All @@ -254,4 +268,41 @@ private function createForm(ColumnTypeInterface $column, $index, $data)

return $this->forms[$formId];
}

/**
* @return string
*/
private function getEntityTypeName()
{
return 'Symfony\Bridge\Doctrine\Form\Type\EntityType';
}

/**
* @return string
*/
private function getDateTimeTypeName()
{
return 'Symfony\Component\Form\Extension\Core\Type\DateTimeType';
}

private function getCollectionTypeName()
{
return 'Symfony\Component\Form\Extension\Core\Type\CollectionType';
}

/**
* @return string
*/
private function getRowTypeName()
{
return 'FSi\Component\DataGrid\Extension\Symfony\ColumnTypeExtension\Symfony3RowType';
}

/**
* @return bool
*/
private function isSymfony3()
{
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
}
}
34 changes: 34 additions & 0 deletions lib/Extension/Symfony/ColumnTypeExtension/Symfony3RowType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FSi\Component\DataGrid\Extension\Symfony\ColumnTypeExtension;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class Symfony3RowType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($options['fields'] as $field) {
$builder->add($field['name'], $field['type'], $field['options']);
}
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('fields', []);
}


/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'row';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function testSimpleBindData()
'editable' => true,
'form_options' => array(),
'form_type' => array(
'name' => array('type' => 'text'),
'author' => array('type' => 'text'),
'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'),
)
));

Expand Down Expand Up @@ -241,4 +241,9 @@ private function getDataMapperReturnCallback()
return $dataMapper;
});
}

private function isSymfony3()
{
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
}
}

0 comments on commit 9c4ec65

Please sign in to comment.