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
41 changes: 40 additions & 1 deletion Serializer/XmlFormViewSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ protected function serializeBlock(\DOMElement $parentElement, FormView $view, $b
}
}

if ($variables['label'])
Copy link

Choose a reason for hiding this comment

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

This is not consistent with the behavior of Symfony. null means that the label should be generated by humanizing the name.
Disabling the label is only done when the value is false (as of Symfony 2.2, never done before)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, good idea

{
Copy link

Choose a reason for hiding this comment

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

CS issue

Copy link

Choose a reason for hiding this comment

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

Please follow the PSR-2 CS.

$this->serializeLabel($parentElement, $type, $variables);
}

if ($view->isRendered()) {
return;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -174,6 +187,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 +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']);

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 Down Expand Up @@ -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);
Expand Down Expand Up @@ -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') }}
Expand All @@ -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)
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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']);
Expand All @@ -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']);
Expand Down Expand Up @@ -567,6 +605,7 @@ protected function serializeDatetimeWidget(\DOMElement $parentElement, FormView
</div>
{% endif %}
*/

Copy link

Choose a reason for hiding this comment

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

Separating the method from its phpdoc is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

involuntary ^^

protected function serializeDateWidget(\DOMElement $parentElement, FormView $view, $variables)
{
if ('single_text' == $variables['widget']) {
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ 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"/>
<input type="text" id="post_title" name="post[title]" required="required"/>
</form>

XML
Expand All @@ -148,7 +148,7 @@ 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"/>
<input type="text" id="post_title" name="post[title]" required="required"/>
</form>

XML
Expand Down Expand Up @@ -184,7 +184,7 @@ 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"/>
<input type="text" id="post_title" name="post[title]" required="required"/>
</form>
</posts>

Expand Down
81 changes: 48 additions & 33 deletions Tests/Functional/Serializer/XmlFormViewSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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(
Expand All @@ -42,7 +46,7 @@ public function test()
),
))
->add('public', 'checkbox', array(
'label' => 'Show this entry publicly?',
'label' => 'label',
'required' => false,
))
->getForm();
Expand All @@ -61,25 +65,33 @@ public function test()

$this->assertXmlElementEquals(<<<XML
<form>
<input type="text" name="form[name]" required="required"/>
<textarea name="form[description]" required="required">Desc</textarea>
<input type="email" name="form[email]" required="required"/>
<input type="integer" name="form[age]" required="required"/>
<input type="number" name="form[height]" required="required"/>
<input type="password" name="form[password]" required="required"/>
<input type="text" name="form[progress]" required="required"/>
<input type="search" name="form[query]" required="required"/>
<input type="url" name="form[website]" required="required"/>
<select name="form[gender]" required="required">
<label for="form_name">custom label</label>
<input type="text" id="form_name" name="form[name]" required="required"/>
<textarea id="form_description" name="form[description]" required="required">Desc</textarea>
<input type="email" id="form_email" name="form[email]" required="required"/>
<input type="integer" id="form_age" name="form[age]" required="required"/>
<input type="number" id="form_height" name="form[height]" required="required"/>
<input type="password" id="form_password" name="form[password]" required="required"/>
<input type="text" id="form_progress" name="form[progress]" required="required"/>
<input type="search" id="form_query" name="form[query]" required="required"/>
<input type="url" id="form_website" name="form[website]" required="required"/>
<label for="form_gender">custom</label>
<select name="form[gender]" required="required" id="form_gender">
<option value="m">male</option>
<option value="f">female</option>
</select>
<input type="radio" value="m" name="form[genderRadio]" required="required"/>
<input type="radio" value="f" name="form[genderRadio]" required="required"/>
<input type="hidden" name="form[limit]"/>
<input type="email" value="Paris" name="form[towns][par]" class="email-box"/>
<input type="email" value="London" name="form[towns][lon]" class="email-box"/>
<input type="checkbox" value="1" checked="1" name="form[public]"/>
<label for="form_genderRadio">label</label>
<fieldset id="form_genderRadio">
<label for="form_genderRadio_0">male</label>
<input type="radio" id="form_genderRadio_0" value="m" name="form[genderRadio]" required="required"/>
<label for="form_genderRadio_1">female</label>
<input type="radio" id="form_genderRadio_1" value="f" name="form[genderRadio]" required="required"/>
</fieldset>
<input type="hidden" id="form_limit" name="form[limit]"/>
<input type="email" id="form_towns_par" value="Paris" name="form[towns][par]" class="email-box"/>
<input type="email" id="form_towns_lon" value="London" name="form[towns][lon]" class="email-box"/>
<label for="form_public">label</label>
<input type="checkbox" id="form_public" value="1" checked="1" name="form[public]"/>
</form>
XML
, $formElement);
Expand All @@ -100,14 +112,17 @@ public function testFileType()

$this->assertXmlElementEquals(<<<XML
<form enctype="multipart/form-data">
<input type="file" name="form[avatar]" required="required"/>
<input type="file" id="form_avatar" name="form[avatar]" required="required"/>
</form>
XML
, $formElement);
}

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(
Expand Down Expand Up @@ -136,7 +151,7 @@ public function testDateFields()

$this->assertXmlElementEquals(<<<XML
<form>
<select name="form[publishedAt][year]" required="required">
<select name="form[publishedAt][year]" required="required" id="form_publishedAt_year">
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
Expand All @@ -149,7 +164,7 @@ public function testDateFields()
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<select name="form[publishedAt][month]" required="required">
<select name="form[publishedAt][month]" required="required" id="form_publishedAt_month">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
Expand All @@ -163,7 +178,7 @@ public function testDateFields()
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select name="form[publishedAt][day]" required="required">
<select name="form[publishedAt][day]" required="required" id="form_publishedAt_day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
Expand Down Expand Up @@ -196,11 +211,11 @@ public function testDateFields()
<option value="30">30</option>
<option value="31">31</option>
</select>
<input type="text" name="form[editedAt][year]" required="required"/>
<input type="text" name="form[editedAt][month]" required="required"/>
<input type="text" name="form[editedAt][day]" required="required"/>
<input type="date" name="form[createdAt]" required="required"/>
<select name="form[publishedAtTime][date][year]" required="required">
<input type="text" id="form_editedAt_year" name="form[editedAt][year]" required="required"/>
<input type="text" id="form_editedAt_month" name="form[editedAt][month]" required="required"/>
<input type="text" id="form_editedAt_day" name="form[editedAt][day]" required="required"/>
<input type="date" id="form_createdAt" name="form[createdAt]" required="required"/>
<select name="form[publishedAtTime][date][year]" required="required" id="form_publishedAtTime_date_year">
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
Expand All @@ -213,7 +228,7 @@ public function testDateFields()
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<select name="form[publishedAtTime][date][month]" required="required">
<select name="form[publishedAtTime][date][month]" required="required" id="form_publishedAtTime_date_month">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
Expand All @@ -227,7 +242,7 @@ public function testDateFields()
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select name="form[publishedAtTime][date][day]" required="required">
<select name="form[publishedAtTime][date][day]" required="required" id="form_publishedAtTime_date_day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
Expand Down Expand Up @@ -260,7 +275,7 @@ public function testDateFields()
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="form[publishedAtTime][time][hour]" required="required">
<select name="form[publishedAtTime][time][hour]" required="required" id="form_publishedAtTime_time_hour">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
Expand All @@ -286,7 +301,7 @@ public function testDateFields()
<option value="22">22</option>
<option value="23">23</option>
</select>
<select name="form[publishedAtTime][time][minute]" required="required">
<select name="form[publishedAtTime][time][minute]" required="required" id="form_publishedAtTime_time_minute">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
Expand Down Expand Up @@ -376,7 +391,7 @@ public function testAttributes()

$this->assertXmlElementEquals(<<<XML
<form method="POST" action="http://localhost/hey" rel="create">
<input type="text" name="form[name]" required="required"/>
<input type="text" id="form_name" name="form[name]" required="required"/>
</form>
XML
, $formElement);
Expand Down