From b4a60dbd16705584e54d9a98d3f32deaa2ca94ae Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Thu, 21 Feb 2013 18:06:47 +0100 Subject: [PATCH 01/13] improve serializer with label/fieldset --- Serializer/XmlFormViewSerializer.php | 41 +++++++++- .../Serializer/XmlFormViewSerializerTest.php | 81 +++++++++++-------- 2 files changed, 88 insertions(+), 34 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 055542e..2b274a3 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -42,6 +42,11 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } + if ($variables['label']) + { + $this->serializeLabel($parentElement, $type, $variables); + } + if ($view->isRendered()) { return; } @@ -78,7 +83,15 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b $this->serializeUrlWidget($parentElement, $view, $variables); break; case 'choice': - $this->serializeChoiceWidget($parentElement, $view, $variables); + if ($variables['expanded']) + { + $this->serializeFieldset($parentElement, $view, $variables); + } + else + { + $this->serializeChoiceWidget($parentElement, $view, $variables); + } + break; case 'hidden': $this->serializeHiddenWidget($parentElement, $view, $variables); @@ -174,6 +187,7 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v $parentElement->appendChild($inputElement); $inputElement->setAttribute('type', $variables['type']); + $inputElement->setAttribute('id', $variables['id']); if (!empty($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); @@ -182,6 +196,15 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v $this->addWidgetAttributes($inputElement, $view, $variables); } + protected function serializeLabel(\DOMElement $parentElement, $type, $variables) + { + $labelElement = $parentElement->ownerDocument->createElement('label',$variables['label']); + $parentElement->appendChild($labelElement); + + $labelElement->setAttribute('for', $variables['id']); + + } + /* id="{{ id }}" name="{{ full_name }}" @@ -237,6 +260,7 @@ protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $vie protected function serializeTextareaWidget(\DOMElement $parentElement, FormView $view, $variables) { $textareaElement = $parentElement->ownerDocument->createElement('textarea', $variables['value']); + $textareaElement->setAttribute('id', $variables['id']); $parentElement->appendChild($textareaElement); $this->addWidgetAttributes($textareaElement, $view, $variables); @@ -320,6 +344,15 @@ protected function serializeUrlWidget(\DOMElement $parentElement, FormView $view $this->serializeWidgetSimple($parentElement, $view, $variables); } + protected function serializeFieldset(\DOMElement $parentElement, FormView $view, $variables) + { + $fieldsetElement = $parentElement->ownerDocument->createElement('fieldset'); + $parentElement->appendChild($fieldsetElement); + + $fieldsetElement->setAttribute('id', $variables['id']); + + $this->serializeChoiceWidget($fieldsetElement, $view, $variables); + } /* {% if expanded %} {{ block('choice_widget_expanded') }} @@ -329,6 +362,7 @@ protected function serializeUrlWidget(\DOMElement $parentElement, FormView $view */ protected function serializeChoiceWidget(\DOMElement $parentElement, FormView $view, $variables) { + return isset($variables['expanded']) && $variables['expanded'] ? $this->serializeChoiceWidgetExpanded($parentElement, $view, $variables) : $this->serializeChoiceWidgetCollapsed($parentElement, $view, $variables) @@ -373,6 +407,8 @@ protected function serializeChoiceWidgetCollapsed(\DOMElement $parentElement, Fo $this->addWidgetAttributes($selectElement, $view, $variables); + $selectElement->setAttribute('id', $variables['id']); + if (isset($variables['multiple']) && $variables['multiple']) { $selectElement->setAttribute('multiple', 'multiple'); } @@ -497,6 +533,7 @@ protected function serializeCheckboxWidget(\DOMElement $parentElement, FormView { $inputElement = $parentElement->ownerDocument->createElement('input'); $inputElement->setAttribute('type', 'checkbox'); + $inputElement->setAttribute('id', $variables['id']); if (isset($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); @@ -518,6 +555,7 @@ protected function serializeRadioWidget(\DOMElement $parentElement, FormView $vi { $inputElement = $parentElement->ownerDocument->createElement('input'); $inputElement->setAttribute('type', 'radio'); + $inputElement->setAttribute('id', $variables['id']); if (isset($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); @@ -567,6 +605,7 @@ protected function serializeDatetimeWidget(\DOMElement $parentElement, FormView {% endif %} */ + protected function serializeDateWidget(\DOMElement $parentElement, FormView $view, $variables) { if ('single_text' == $variables['widget']) { diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index fa663a6..153853e 100644 --- a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php +++ b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php @@ -14,7 +14,9 @@ public function test() { $formFactory = $this->getKernel()->getContainer()->get('form.factory'); $form = $formFactory->createBuilder('form') - ->add('name', 'text') + ->add('name', 'text',array( + 'label' => 'custom label' + )) ->add('description', 'textarea') ->add('email', 'email') ->add('age', 'integer') @@ -24,12 +26,14 @@ public function test() ->add('query', 'search') ->add('website', 'url') ->add('gender', 'choice', array( - 'choices' => array('m' => 'male', 'f' => 'female') + 'choices' => array('m' => 'male', 'f' => 'female'), + 'label' => 'custom' )) ->add('genderRadio', 'choice', array( 'choices' => array('m' => 'male', 'f' => 'female'), 'expanded' => true, 'multiple' => false, + 'label' => 'label' )) ->add('limit', 'hidden') ->add('towns', 'collection', array( @@ -42,7 +46,7 @@ public function test() ), )) ->add('public', 'checkbox', array( - 'label' => 'Show this entry publicly?', + 'label' => 'label', 'required' => false, )) ->getForm(); @@ -61,25 +65,33 @@ public function test() $this->assertXmlElementEquals(<< - - - - - - - - - - + + + + + + + + + + - - - - - - + +
+ + + + +
+ + + + + XML , $formElement); @@ -100,7 +112,7 @@ public function testFileType() $this->assertXmlElementEquals(<< - + XML , $formElement); @@ -108,6 +120,9 @@ public function testFileType() 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( @@ -136,7 +151,7 @@ public function testDateFields() $this->assertXmlElementEquals(<< - @@ -149,7 +164,7 @@ public function testDateFields() - @@ -163,7 +178,7 @@ public function testDateFields() - @@ -196,11 +211,11 @@ public function testDateFields() - - - - - + + + + - @@ -227,7 +242,7 @@ public function testDateFields() - @@ -260,7 +275,7 @@ public function testDateFields() - @@ -286,7 +301,7 @@ public function testDateFields() - @@ -376,7 +391,7 @@ public function testAttributes() $this->assertXmlElementEquals(<< - + XML , $formElement); From 50fceb3267eb21761482931b784eff35b326321e Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Thu, 21 Feb 2013 18:46:05 +0100 Subject: [PATCH 02/13] fix forgotten tests --- Tests/Functional/ControllerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Functional/ControllerTest.php b/Tests/Functional/ControllerTest.php index ce0ecd1..341e625 100644 --- a/Tests/Functional/ControllerTest.php +++ b/Tests/Functional/ControllerTest.php @@ -129,7 +129,7 @@ public function testGetCreatePostFormXml()
- +
XML @@ -148,7 +148,7 @@ public function testGetCreatePostWithFormatInLinksFormXml()
- +
XML @@ -184,7 +184,7 @@ public function testListPostsXml()
- +
From 0b68342860123426ec2781de5ecc126330479886 Mon Sep 17 00:00:00 2001 From: Nicolas Badey Date: Thu, 21 Feb 2013 21:22:50 +0100 Subject: [PATCH 03/13] fix CS --- Serializer/XmlFormViewSerializer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 2b274a3..3e6d534 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -605,7 +605,6 @@ protected function serializeDatetimeWidget(\DOMElement $parentElement, FormView {% endif %} */ - protected function serializeDateWidget(\DOMElement $parentElement, FormView $view, $variables) { if ('single_text' == $variables['widget']) { From 09f8bf28f6dc40004aff8b210032aae5c7a3b5ac Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Fri, 22 Feb 2013 13:25:08 +0100 Subject: [PATCH 04/13] add label default value --- Serializer/XmlFormViewSerializer.php | 20 ++++++++++-- Tests/Functional/ControllerTest.php | 3 ++ .../Serializer/XmlFormViewSerializerTest.php | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 3e6d534..b832271 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -42,9 +42,9 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } - if ($variables['label']) + if ($variables['label'] === null) { - $this->serializeLabel($parentElement, $type, $variables); + $variables['label'] = $this->humanize($variables['name']); } if ($view->isRendered()) { @@ -54,6 +54,12 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b if ('rest' == $blockName) { $this->serializeRestWidget($parentElement, $view, $variables); } else { + + if (($type || 'widget' == $blockName) && false !== $variables['label']) + { + $this->serializeLabel($parentElement, $type, $variables); + } + switch ($type) { case 'text': $this->serializeWidgetSimple($parentElement, $view, $variables); @@ -630,4 +636,14 @@ protected function isSelectedChoice(ChoiceView $choice, $selectedValue) return $choice->value === $selectedValue; } + + /** + * Copied from the symfony src code + * + * @see Symfony\Component\Form\FormRenderer::humanize + */ + public function humanize($text) + { + return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); + } } diff --git a/Tests/Functional/ControllerTest.php b/Tests/Functional/ControllerTest.php index 341e625..746cf30 100644 --- a/Tests/Functional/ControllerTest.php +++ b/Tests/Functional/ControllerTest.php @@ -129,6 +129,7 @@ public function testGetCreatePostFormXml()
+
@@ -148,6 +149,7 @@ public function testGetCreatePostWithFormatInLinksFormXml()
+
@@ -184,6 +186,7 @@ public function testListPostsXml()
+
diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index 153853e..896cc82 100644 --- a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php +++ b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php @@ -67,13 +67,21 @@ public function test()
+ + + + + + + + + + + + @@ -112,6 +124,7 @@ public function testFileType() $this->assertXmlElementEquals(<< + XML @@ -151,6 +164,8 @@ public function testDateFields() $this->assertXmlElementEquals(<< + + + + + + + + + + + + + + + + + XML From 3f24d4a56887daee7c86ab1de175e73f0405e2ca Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Fri, 22 Feb 2013 13:27:23 +0100 Subject: [PATCH 05/13] CS fix --- Serializer/XmlFormViewSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index b832271..24f8e84 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -42,7 +42,7 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } - if ($variables['label'] === null) + if (null === $variables['label']) { $variables['label'] = $this->humanize($variables['name']); } From 45a48c992edd9238458cebca85982e5c7cc52bb6 Mon Sep 17 00:00:00 2001 From: Nico-B Date: Fri, 22 Feb 2013 23:01:54 +0100 Subject: [PATCH 06/13] PSR-2 --- Serializer/XmlFormViewSerializer.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 24f8e84..4c722a1 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -42,8 +42,7 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } - if (null === $variables['label']) - { + if (null === $variables['label']) { $variables['label'] = $this->humanize($variables['name']); } @@ -55,8 +54,7 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b $this->serializeRestWidget($parentElement, $view, $variables); } else { - if (($type || 'widget' == $blockName) && false !== $variables['label']) - { + if (($type || 'widget' == $blockName) && false !== $variables['label']) { $this->serializeLabel($parentElement, $type, $variables); } @@ -89,12 +87,9 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b $this->serializeUrlWidget($parentElement, $view, $variables); break; case 'choice': - if ($variables['expanded']) - { + if ($variables['expanded']) { $this->serializeFieldset($parentElement, $view, $variables); - } - else - { + } else { $this->serializeChoiceWidget($parentElement, $view, $variables); } @@ -130,7 +125,6 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } - } $view->setRendered(); @@ -169,8 +163,7 @@ protected function serializeFormRow(\DOMElement $parentElement, FormView $view, /* {% if compound %} {{ block('form_widget_compound') }} - {% else %} - {{ block('form_widget_simple') }} + {% else { %}{ block('form_widget_simple') }} {% endif %} */ protected function serializeFormWidget(\DOMElement $parentElement, FormView $view, $variables) @@ -222,8 +215,7 @@ protected function serializeLabel(\DOMElement $parentElement, $type, $variables) {% for attrname, attrvalue in attr %} {% if attrname in ['placeholder', 'title'] %} {{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" - {% else %} - {{ attrname }}="{{ attrvalue }}" + {% else { %}{ attrname }}="{{ attrvalue }}" {% endif %} {% endfor %} */ @@ -362,13 +354,11 @@ protected function serializeFieldset(\DOMElement $parentElement, FormView $view, /* {% if expanded %} {{ block('choice_widget_expanded') }} - {% else %} - {{ block('choice_widget_collapsed') }} + {% else { %}{ block('choice_widget_collapsed') }} {% endif %} */ protected function serializeChoiceWidget(\DOMElement $parentElement, FormView $view, $variables) { - return isset($variables['expanded']) && $variables['expanded'] ? $this->serializeChoiceWidgetExpanded($parentElement, $view, $variables) : $this->serializeChoiceWidgetCollapsed($parentElement, $view, $variables) From ccd8e26dc597bcc37e3f915704df3553efe87c8f Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 11:25:49 +0200 Subject: [PATCH 07/13] WIP translation --- Resources/config/services.yml | 2 ++ Serializer/XmlFormViewSerializer.php | 14 ++++++++++++-- .../Serializer/XmlFormViewSerializerTest.php | 12 ++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 4a0f4ea..749c0d2 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -54,6 +54,8 @@ services: fsc_hateoas.serializer.xml_form_view: class: %fsc_hateoas.serializer.xml_form_view.class% + arguments: + - @translator fsc_hateoas.serializer.event_subscriber.link: class: %fsc_hateoas.serializer.event_subscriber.link.class% diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 4c722a1..510c486 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -4,9 +4,17 @@ use Symfony\Component\Form\FormView; use Symfony\Component\Form\Extension\Core\View\ChoiceView; +use Symfony\Component\Translation\TranslatorInterface; class XmlFormViewSerializer { + protected $transaltor; + + public function __construct (TranslatorInterface $transaltor) + { + $this->transaltor = $transaltor; + } + protected static $baseTypes = array( 'text', 'textarea', 'email', 'integer', 'money', 'number', 'password', 'percent', 'search', 'url', 'hidden', 'collection', 'choice', 'checkbox', 'radio', 'datetime', 'date', @@ -197,7 +205,8 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v protected function serializeLabel(\DOMElement $parentElement, $type, $variables) { - $labelElement = $parentElement->ownerDocument->createElement('label',$variables['label']); + $translatedLabel = $this->transaltor->trans($variables['label']); + $labelElement = $parentElement->ownerDocument->createElement('label',$translatedLabel); $parentElement->appendChild($labelElement); $labelElement->setAttribute('for', $variables['id']); @@ -452,7 +461,8 @@ protected function serializeChoiceWidgetOptions(\DOMElement $selectElement, Form 'options' => $choiceView, ))); } else { - $optionElement = $selectElement->ownerDocument->createElement('option', $choiceView->label); + $translatedLabel = $this->transaltor->trans($choiceView->label); + $optionElement = $selectElement->ownerDocument->createElement('option', $translatedLabel); $optionElement->setAttribute('value', $choiceView->value); if ($this->isSelectedChoice($choiceView, $variables['value'])) { diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index 896cc82..3a8fa0b 100644 --- a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php +++ b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php @@ -59,7 +59,8 @@ public function test() $formView = $form->createView(); - $xmlFormViewSerializer = new XmlFormViewSerializer(); + $translator = $this->getKernel()->getContainer()->get('translator'); + $xmlFormViewSerializer = new XmlFormViewSerializer($translator); $xmlFormViewSerializer->serialize($formView, $formElement = $this->createFormElement()); @@ -118,7 +119,8 @@ public function testFileType() $formView = $form->createView(); - $xmlFormViewSerializer = new XmlFormViewSerializer(); + $translator = $this->getKernel()->getContainer()->get('translator'); + $xmlFormViewSerializer = new XmlFormViewSerializer($translator); $xmlFormViewSerializer->serialize($formView, $formElement = $this->createFormElement()); @@ -158,7 +160,8 @@ public function testDateFields() $formView = $form->createView(); - $xmlFormViewSerializer = new XmlFormViewSerializer(); + $translator = $this->getKernel()->getContainer()->get('translator'); + $xmlFormViewSerializer = new XmlFormViewSerializer($translator); $xmlFormViewSerializer->serialize($formView, $formElement = $this->createFormElement()); @@ -415,7 +418,8 @@ public function testAttributes() 'rel' => 'create', ); - $xmlFormViewSerializer = new XmlFormViewSerializer(); + $translator = $this->getKernel()->getContainer()->get('translator'); + $xmlFormViewSerializer = new XmlFormViewSerializer($translator); $xmlFormViewSerializer->serialize($formView, $formElement = $this->createFormElement()); From 0d580d3c474c590a048e29cc7857088767107258 Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 11:34:04 +0200 Subject: [PATCH 08/13] WIP translation empty_value --- Serializer/XmlFormViewSerializer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 510c486..b3be03e 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -419,7 +419,8 @@ protected function serializeChoiceWidgetCollapsed(\DOMElement $parentElement, Fo } if (isset($variables['empty_value']) && null !== $variables['empty_value']) { - $noneOptionElement = $selectElement->ownerDocument->createElement('option', $variables['empty_value']); + $translatedEmpty_value = $this->transaltor->trans($variables['empty_value']); + $noneOptionElement = $selectElement->ownerDocument->createElement('option', $translatedEmpty_value); $noneOptionElement->setAttribute('value', ''); $selectElement->appendChild($noneOptionElement); From 16f079eff7cefe37908d6de4cfe2f182c56e760b Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 11:38:39 +0200 Subject: [PATCH 09/13] PHP-CS don't like twig in comments --- Serializer/XmlFormViewSerializer.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index b3be03e..1ca6e64 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -171,7 +171,8 @@ protected function serializeFormRow(\DOMElement $parentElement, FormView $view, /* {% if compound %} {{ block('form_widget_compound') }} - {% else { %}{ block('form_widget_simple') }} + {% else %} + {{ block('form_widget_simple') }} {% endif %} */ protected function serializeFormWidget(\DOMElement $parentElement, FormView $view, $variables) @@ -224,7 +225,8 @@ protected function serializeLabel(\DOMElement $parentElement, $type, $variables) {% for attrname, attrvalue in attr %} {% if attrname in ['placeholder', 'title'] %} {{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" - {% else { %}{ attrname }}="{{ attrvalue }}" + {% else %} + {{ attrname }}="{{ attrvalue }}" {% endif %} {% endfor %} */ @@ -363,7 +365,8 @@ protected function serializeFieldset(\DOMElement $parentElement, FormView $view, /* {% if expanded %} {{ block('choice_widget_expanded') }} - {% else { %}{ block('choice_widget_collapsed') }} + {% else %} + {{ block('choice_widget_collapsed') }} {% endif %} */ protected function serializeChoiceWidget(\DOMElement $parentElement, FormView $view, $variables) From ac1ba080793bf4dd94b722a90b0eaca9cfc68a4e Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 11:56:13 +0200 Subject: [PATCH 10/13] addWidgetAttributes --- Serializer/XmlFormViewSerializer.php | 8 +-- Tests/Functional/ControllerTest.php | 6 +- .../Serializer/XmlFormViewSerializerTest.php | 60 +++++++++---------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 1ca6e64..ba8982e 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -195,7 +195,6 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v $parentElement->appendChild($inputElement); $inputElement->setAttribute('type', $variables['type']); - $inputElement->setAttribute('id', $variables['id']); if (!empty($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); @@ -234,6 +233,8 @@ protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $vie { $widgetElement->setAttribute('name', $variables['full_name']); + $widgetElement->setAttribute('id', $variables['id']); + if ($variables['read_only']) { $widgetElement->setAttribute('readonly', 'readonly'); } @@ -269,7 +270,6 @@ protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $vie protected function serializeTextareaWidget(\DOMElement $parentElement, FormView $view, $variables) { $textareaElement = $parentElement->ownerDocument->createElement('textarea', $variables['value']); - $textareaElement->setAttribute('id', $variables['id']); $parentElement->appendChild($textareaElement); $this->addWidgetAttributes($textareaElement, $view, $variables); @@ -415,8 +415,6 @@ protected function serializeChoiceWidgetCollapsed(\DOMElement $parentElement, Fo $this->addWidgetAttributes($selectElement, $view, $variables); - $selectElement->setAttribute('id', $variables['id']); - if (isset($variables['multiple']) && $variables['multiple']) { $selectElement->setAttribute('multiple', 'multiple'); } @@ -543,7 +541,6 @@ protected function serializeCheckboxWidget(\DOMElement $parentElement, FormView { $inputElement = $parentElement->ownerDocument->createElement('input'); $inputElement->setAttribute('type', 'checkbox'); - $inputElement->setAttribute('id', $variables['id']); if (isset($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); @@ -565,7 +562,6 @@ protected function serializeRadioWidget(\DOMElement $parentElement, FormView $vi { $inputElement = $parentElement->ownerDocument->createElement('input'); $inputElement->setAttribute('type', 'radio'); - $inputElement->setAttribute('id', $variables['id']); if (isset($variables['value'])) { $inputElement->setAttribute('value', $variables['value']); diff --git a/Tests/Functional/ControllerTest.php b/Tests/Functional/ControllerTest.php index 746cf30..6c8e550 100644 --- a/Tests/Functional/ControllerTest.php +++ b/Tests/Functional/ControllerTest.php @@ -130,7 +130,7 @@ public function testGetCreatePostFormXml()
- +
XML @@ -150,7 +150,7 @@ public function testGetCreatePostWithFormatInLinksFormXml()
- +
XML @@ -187,7 +187,7 @@ public function testListPostsXml()
- +
diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index 3a8fa0b..a25558c 100644 --- a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php +++ b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php @@ -67,44 +67,44 @@ public function test() $this->assertXmlElementEquals(<< - + - + - + - + - + - + - + - + - + -
- + - +
- + - + - + - + XML , $formElement); @@ -127,7 +127,7 @@ public function testFileType() $this->assertXmlElementEquals(<< - + XML , $formElement); @@ -169,7 +169,7 @@ public function testDateFields()
- @@ -183,7 +183,7 @@ public function testDateFields() - @@ -198,7 +198,7 @@ public function testDateFields() - @@ -233,17 +233,17 @@ public function testDateFields() - + - + - + - + - @@ -257,7 +257,7 @@ public function testDateFields() - @@ -272,7 +272,7 @@ public function testDateFields() - @@ -307,7 +307,7 @@ public function testDateFields() - @@ -334,7 +334,7 @@ public function testDateFields() - @@ -426,7 +426,7 @@ public function testAttributes() $this->assertXmlElementEquals(<< - + XML , $formElement); From 3dc02f5ff05c3e692097a9f21369a30beb232f7b Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 11:58:33 +0200 Subject: [PATCH 11/13] translate placeholder --- Serializer/XmlFormViewSerializer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index ba8982e..0f04e78 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -8,11 +8,11 @@ class XmlFormViewSerializer { - protected $transaltor; + protected $translator; - public function __construct (TranslatorInterface $transaltor) + public function __construct (TranslatorInterface $translator) { - $this->transaltor = $transaltor; + $this->translator = $translator; } protected static $baseTypes = array( @@ -205,7 +205,7 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v protected function serializeLabel(\DOMElement $parentElement, $type, $variables) { - $translatedLabel = $this->transaltor->trans($variables['label']); + $translatedLabel = $this->translator->trans($variables['label']); $labelElement = $parentElement->ownerDocument->createElement('label',$translatedLabel); $parentElement->appendChild($labelElement); @@ -257,7 +257,7 @@ protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $vie foreach ($variables['attr'] as $name => $value) { if (in_array($name, array('placeholder', 'title'))) { - // TODO Translate the thing ... + $this->translator->trans($value); } $widgetElement->setAttribute($name, $value); @@ -420,7 +420,7 @@ protected function serializeChoiceWidgetCollapsed(\DOMElement $parentElement, Fo } if (isset($variables['empty_value']) && null !== $variables['empty_value']) { - $translatedEmpty_value = $this->transaltor->trans($variables['empty_value']); + $translatedEmpty_value = $this->translator->trans($variables['empty_value']); $noneOptionElement = $selectElement->ownerDocument->createElement('option', $translatedEmpty_value); $noneOptionElement->setAttribute('value', ''); @@ -463,7 +463,7 @@ protected function serializeChoiceWidgetOptions(\DOMElement $selectElement, Form 'options' => $choiceView, ))); } else { - $translatedLabel = $this->transaltor->trans($choiceView->label); + $translatedLabel = $this->translator->trans($choiceView->label); $optionElement = $selectElement->ownerDocument->createElement('option', $translatedLabel); $optionElement->setAttribute('value', $choiceView->value); From 5f2d5600cf25f60743ff3bb7f0b9bd9acdd634e8 Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 12:04:40 +0200 Subject: [PATCH 12/13] transalte place holder fix typo --- Serializer/XmlFormViewSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index 0f04e78..d1cbad1 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -257,7 +257,7 @@ protected function addWidgetAttributes(\DOMElement $widgetElement, FormView $vie foreach ($variables['attr'] as $name => $value) { if (in_array($name, array('placeholder', 'title'))) { - $this->translator->trans($value); + $value = $this->translator->trans($value); } $widgetElement->setAttribute($name, $value); From 62b7001bff0af393c6b34cd13bcaddd26c7185a5 Mon Sep 17 00:00:00 2001 From: NicolasBadey Date: Wed, 3 Apr 2013 12:43:16 +0200 Subject: [PATCH 13/13] move fieldset --- Serializer/XmlFormViewSerializer.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Serializer/XmlFormViewSerializer.php b/Serializer/XmlFormViewSerializer.php index d1cbad1..0244b25 100644 --- a/Serializer/XmlFormViewSerializer.php +++ b/Serializer/XmlFormViewSerializer.php @@ -95,12 +95,7 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b $this->serializeUrlWidget($parentElement, $view, $variables); break; case 'choice': - if ($variables['expanded']) { - $this->serializeFieldset($parentElement, $view, $variables); - } else { - $this->serializeChoiceWidget($parentElement, $view, $variables); - } - + $this->serializeChoiceWidget($parentElement, $view, $variables); break; case 'hidden': $this->serializeHiddenWidget($parentElement, $view, $variables); @@ -360,7 +355,7 @@ protected function serializeFieldset(\DOMElement $parentElement, FormView $view, $fieldsetElement->setAttribute('id', $variables['id']); - $this->serializeChoiceWidget($fieldsetElement, $view, $variables); + $this->serializeChoiceWidgetExpanded($fieldsetElement, $view, $variables); } /* {% if expanded %} @@ -372,7 +367,7 @@ protected function serializeFieldset(\DOMElement $parentElement, FormView $view, protected function serializeChoiceWidget(\DOMElement $parentElement, FormView $view, $variables) { return isset($variables['expanded']) && $variables['expanded'] - ? $this->serializeChoiceWidgetExpanded($parentElement, $view, $variables) + ? $this->serializeFieldset($parentElement, $view, $variables) : $this->serializeChoiceWidgetCollapsed($parentElement, $view, $variables) ; }