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

Add support for label/fieldset in serializer #30

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
59 changes: 54 additions & 5 deletions Serializer/XmlFormViewSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -42,13 +50,22 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b
}
}

if (null === $variables['label']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$variables['label'] = $this->humanize($variables['name']);
}

if ($view->isRendered()) {
return;
}

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);
Expand Down Expand Up @@ -111,7 +128,6 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b
}
}


}

$view->setRendered();
Expand Down Expand Up @@ -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']);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

}

/*
id="{{ id }}"
name="{{ full_name }}"
Expand All @@ -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');
}
Expand All @@ -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);
Expand Down Expand Up @@ -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') }}
Expand All @@ -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)
;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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))));
}
}
9 changes: 6 additions & 3 deletions Tests/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public function testGetCreatePostFormXml()
<?xml version="1.0" encoding="UTF-8"?>
<form method="POST" action="http://localhost/api/posts">
<link rel="self" href="http://localhost/api/posts/create?_format=xml"/>
<input type="text" name="post[title]" required="required"/>
<label for="post_title">Title</label>
<input type="text" name="post[title]" id="post_title" required="required"/>
</form>

XML
Expand All @@ -148,7 +149,8 @@ public function testGetCreatePostWithFormatInLinksFormXml()
<?xml version="1.0" encoding="UTF-8"?>
<form method="POST" action="http://localhost/api/posts?_format=xml">
<link rel="self" href="http://localhost/api/posts/create_format?_format=xml"/>
<input type="text" name="post[title]" required="required"/>
<label for="post_title">Title</label>
<input type="text" name="post[title]" id="post_title" required="required"/>
</form>

XML
Expand Down Expand Up @@ -184,7 +186,8 @@ public function testListPostsXml()
<link rel="create" href="http://localhost/api/posts/create"/>
<form rel="create" method="POST" action="http://localhost/api/posts">
<link rel="self" href="http://localhost/api/posts/create"/>
<input type="text" name="post[title]" required="required"/>
<label for="post_title">Title</label>
<input type="text" name="post[title]" id="post_title" required="required"/>
</form>
</posts>

Expand Down
Loading