diff --git a/.travis.yml b/.travis.yml index ac9f3a8..4db50ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 055542e..9b053c0 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -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) { @@ -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); } /* diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index fa663a6..2541454 100644 --- a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php +++ b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php @@ -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 @@ -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(<< +