Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix collection's serialization #27

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:

before_script:
- wget http://getcomposer.org/composer.phar
- php composer.phar require symfony/framework-bundle:${SYMFONY_VERSION}
- php composer.phar require symfony/symfony:${SYMFONY_VERSION}
- php composer.phar --dev install

script:
Expand Down
62 changes: 41 additions & 21 deletions Serializer/XmlFormViewSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,30 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v
*/
protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $view, $variables)
{
$widgetElement->setAttribute('name', $variables['full_name']);
if (!isset($variables['prototype'])) {
$widgetElement->setAttribute('name', $variables['full_name']);

if ($variables['read_only']) {
$widgetElement->setAttribute('readonly', 'readonly');
}
if ($variables['read_only']) {
$widgetElement->setAttribute('readonly', 'readonly');
}

if ($variables['disabled']) {
$widgetElement->setAttribute('disabled', 'disabled');
}
if ($variables['disabled']) {
$widgetElement->setAttribute('disabled', 'disabled');
}

if ($variables['required']) {
$widgetElement->setAttribute('required', 'required');
}
if ($variables['required']) {
$widgetElement->setAttribute('required', 'required');
}

if ($variables['max_length']) {
$widgetElement->setAttribute('maxlength', $variables['max_length']);
}
if ($variables['max_length']) {
$widgetElement->setAttribute('maxlength', $variables['max_length']);
}

if ($variables['pattern']) {
$widgetElement->setAttribute('pattern', $variables['pattern']);
if ($variables['pattern']) {
$widgetElement->setAttribute('pattern', $variables['pattern']);
}
} else {
$widgetElement->setAttribute('id', $variables['id']);
}

foreach ($variables['attr'] as $name => $value) {
Expand Down Expand Up @@ -480,14 +484,30 @@ protected function serializeFormRows(\DOMElement $parentElement, FormView $view,
protected function serializeCollectionWidget(\DOMElement $parentElement, FormView $view, $variables)
{
if (isset($variables['prototype'])) {
// TODO test this ?
var_dump($variables['prototype']);exit;
$variables['attr'] = array_merge($variables['attr'], array(
'data-prototype' => $this->renderer->searchAndRenderBlock($variables['prototype'], 'row'),
));
$this->serializePrototype($parentElement, $view, $variables);
}

$this->serializeFormWidget($parentElement, $view, $variables);

}

protected function serializePrototype(\DOMElement $parentElement, FormView $view, $variables)
{
$document = new \DOMDocument('1.0', 'UTF-8');

$divElement = $document->createElement('div');
$divElement->setAttribute('id',$variables['id'].'___name__');

$this->serializeBlock($divElement,$variables['prototype'], 'row');

$ulElement = $parentElement->ownerDocument->createElement('ul');

$variables['attr'] = array_merge($variables['attr'], array(
'data-prototype' => htmlentities($document->saveHTML($divElement), ENT_QUOTES, 'UTF-8')
));

$this->addWidgetAttributes($ulElement, $view, $variables);

$parentElement->appendChild($ulElement);
}

/*
Expand Down
52 changes: 52 additions & 0 deletions Tests/Functional/Serializer/XmlFormViewSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use FSC\HateoasBundle\Tests\Functional\TestCase;
use FSC\HateoasBundle\Serializer\XmlFormViewSerializer;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


/**
* @group functional
Expand Down Expand Up @@ -106,8 +110,36 @@ public function testFileType()
, $formElement);
}

public function testCollectionType()
{
$formFactory = $this->getKernel()->getContainer()->get('form.factory');

$form = $formFactory->createBuilder('form')
->add('service', 'collection', array(
'type' => new availabilityFormType(),
'allow_add' => true
))
->getForm();

$formView = $form->createView();

$xmlFormViewSerializer = new XmlFormViewSerializer();

$xmlFormViewSerializer->serialize($formView, $formElement = $this->createFormElement());

$this->assertXmlElementEquals(<<<XML
<form>
<ul id="form_service" data-prototype="&amp;lt;div id=&amp;quot;form_service___name__&amp;quot;&amp;gt;&#10;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;form[service][__name__][timeId]&amp;quot; required=&amp;quot;required&amp;quot;&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;form[service][__name__][dayId]&amp;quot; required=&amp;quot;required&amp;quot;&amp;gt;&#10;&amp;lt;/div&amp;gt;"/>
</form>
XML
, $formElement);
}

public function testDateFields()
{
// force locale for PHP_INTL DateTime
locale_set_default('en-US');

$formFactory = $this->getKernel()->getContainer()->get('form.factory');
$form = $formFactory->createBuilder('form')
->add('publishedAt', 'date', array(
Expand Down Expand Up @@ -398,3 +430,23 @@ protected function createFormElement()
return $domDocument->createElement('form');
}
}

class availabilityFormType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('timeId');
$builder->add('dayId');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array());
}
public function getName()
{
return 'availability';
}

}
1 change: 1 addition & 0 deletions Tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ framework:
resource: "%kernel.root_dir%/config/routing.yml"
form: ~
validation: ~
csrf_protection: false