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
60 changes: 52 additions & 8 deletions Serializer/XmlFormViewSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,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 @@ -78,7 +87,12 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b
$this->serializeUrlWidget($parentElement, $view, $variables);
break;
case 'choice':
$this->serializeChoiceWidget($parentElement, $view, $variables);
if ($variables['expanded']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO this check doesn't belong in this method. It should probably be in serializeChoiceWidget

$this->serializeFieldset($parentElement, $view, $variables);
} else {
$this->serializeChoiceWidget($parentElement, $view, $variables);
}

break;
case 'hidden':
$this->serializeHiddenWidget($parentElement, $view, $variables);
Expand Down Expand Up @@ -111,7 +125,6 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b
}
}


}

$view->setRendered();
Expand Down Expand Up @@ -150,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') }}
Copy link
Contributor

Choose a reason for hiding this comment

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

???

{% endif %}
*/
protected function serializeFormWidget(\DOMElement $parentElement, FormView $view, $variables)
Expand All @@ -174,6 +186,7 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v
$parentElement->appendChild($inputElement);

$inputElement->setAttribute('type', $variables['type']);
$inputElement->setAttribute('id', $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.

This should only be done once in addWidgetAttributes


if (!empty($variables['value'])) {
$inputElement->setAttribute('value', $variables['value']);
Expand All @@ -182,6 +195,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']);

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 @@ -193,8 +215,7 @@ protected function serializeWidgetSimple(\DOMElement $parentElement, FormView $v
{% for attrname, attrvalue in attr %}
{% if attrname in ['placeholder', 'title'] %}
{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
{% else %}
{{ attrname }}="{{ attrvalue }}"
{% else { %}{ attrname }}="{{ attrvalue }}"
{% endif %}
{% endfor %}
*/
Expand Down Expand Up @@ -237,6 +258,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);
Expand Down Expand Up @@ -320,11 +342,19 @@ 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') }}
{% else %}
{{ block('choice_widget_collapsed') }}
{% else { %}{ block('choice_widget_collapsed') }}
{% endif %}
*/
protected function serializeChoiceWidget(\DOMElement $parentElement, FormView $view, $variables)
Expand Down Expand Up @@ -373,6 +403,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');
}
Expand Down Expand Up @@ -497,6 +529,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']);
Expand All @@ -518,6 +551,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']);
Expand Down Expand Up @@ -592,4 +626,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" id="post_title" name="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" id="post_title" name="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" id="post_title" name="post[title]" required="required"/>
</form>
</posts>

Expand Down
Loading