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 055542e..0244b25 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 $translator; + + public function __construct (TranslatorInterface $translator) + { + $this->translator = $translator; + } + protected static $baseTypes = array( 'text', 'textarea', 'email', 'integer', 'money', 'number', 'password', 'percent', 'search', 'url', 'hidden', 'collection', 'choice', 'checkbox', 'radio', 'datetime', 'date', @@ -42,6 +50,10 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } + if (null === $variables['label']) { + $variables['label'] = $this->humanize($variables['name']); + } + if ($view->isRendered()) { return; } @@ -49,6 +61,11 @@ 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); @@ -111,7 +128,6 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b } } - } $view->setRendered(); @@ -182,6 +198,16 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v $this->addWidgetAttributes($inputElement, $view, $variables); } + protected function serializeLabel(\DOMElement $parentElement, $type, $variables) + { + $translatedLabel = $this->translator->trans($variables['label']); + $labelElement = $parentElement->ownerDocument->createElement('label',$translatedLabel); + $parentElement->appendChild($labelElement); + + $labelElement->setAttribute('for', $variables['id']); + + } + /* id="{{ id }}" name="{{ full_name }}" @@ -202,6 +228,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'); } @@ -224,7 +252,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 ... + $value = $this->translator->trans($value); } $widgetElement->setAttribute($name, $value); @@ -320,6 +348,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->serializeChoiceWidgetExpanded($fieldsetElement, $view, $variables); + } /* {% if expanded %} {{ block('choice_widget_expanded') }} @@ -330,7 +367,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->serializeFieldset($parentElement, $view, $variables) : $this->serializeChoiceWidgetCollapsed($parentElement, $view, $variables) ; } @@ -378,7 +415,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->translator->trans($variables['empty_value']); + $noneOptionElement = $selectElement->ownerDocument->createElement('option', $translatedEmpty_value); $noneOptionElement->setAttribute('value', ''); $selectElement->appendChild($noneOptionElement); @@ -420,7 +458,8 @@ protected function serializeChoiceWidgetOptions(\DOMElement $selectElement, Form 'options' => $choiceView, ))); } else { - $optionElement = $selectElement->ownerDocument->createElement('option', $choiceView->label); + $translatedLabel = $this->translator->trans($choiceView->label); + $optionElement = $selectElement->ownerDocument->createElement('option', $translatedLabel); $optionElement->setAttribute('value', $choiceView->value); if ($this->isSelectedChoice($choiceView, $variables['value'])) { @@ -592,4 +631,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 ce0ecd1..6c8e550 100644 --- a/Tests/Functional/ControllerTest.php +++ b/Tests/Functional/ControllerTest.php @@ -129,7 +129,8 @@ public function testGetCreatePostFormXml()
XML @@ -148,7 +149,8 @@ public function testGetCreatePostWithFormatInLinksFormXml() XML @@ -184,7 +186,8 @@ public function testListPostsXml() diff --git a/Tests/Functional/Serializer/XmlFormViewSerializerTest.php b/Tests/Functional/Serializer/XmlFormViewSerializerTest.php index fa663a6..a25558c 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(); @@ -55,31 +59,52 @@ 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()); $this->assertXmlElementEquals(<<