Skip to content

[WebProfilerBundle][Form] Add information about form synchronization err... #11487

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,17 @@
<div class="toggle-icon empty"></div>
{% endif %}
{{ name }}
{% if data.errors is defined and data.errors|length > 0 %}
<div class="badge-error">{{ data.errors|length }}</div>

{% set errorsCount = 0 %}
{% if data.errors is defined %}
{% set errorsCount = errorsCount + data.errors|length %}
{% endif %}
{% if data.synchronization_failure_cause is defined %}
{% set errorsCount = errorsCount + 1 %}
{% endif %}

{% if errorsCount > 0 %}
<div class="badge-error">{{ errorsCount }}</div>
{% endif %}
</div>

Expand Down Expand Up @@ -488,6 +497,26 @@
</div>
{% endif %}

{% if data.synchronization_failure_cause is defined %}
<div class="errors">
<h3>
<a class="toggle-button" data-toggle-target-id="{{ data.id }}-synchronized" href="#">
Synchronization failure
<span class="toggle-icon"></span>
</a>
</h3>

<div id="{{ data.id }}-synchronized">
<table>
<tr>
<th width="180">Cause</th>
<td>{{ data.synchronization_failure_cause }}</td>
</tr>
</table>
</div>
</div>
{% endif %}

{% if data.default_data is defined %}
<h3>
<a class="toggle-button" data-toggle-target-id="{{ data.id }}-default_data" href="#">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public function collectSubmittedData(FormInterface $form)
$this->data['nb_errors'] += count($this->dataByForm[$hash]['errors']);
}

// Count synchronization failures
if (isset($this->dataByForm[$hash]['synchronization_failure_cause'])) {
$this->data['nb_errors'] += 1;
}

foreach ($form as $child) {
$this->collectSubmittedData($child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Extension\DataCollector;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
Expand Down Expand Up @@ -134,6 +135,10 @@ public function extractSubmittedData(FormInterface $form)

$data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());

if ($form instanceof Form && null !== $form->getSynchronizationFailureCause()) {
$data['synchronization_failure_cause'] = $form->getSynchronizationFailureCause()->getMessage();
}

return $data;
}

Expand Down
20 changes: 15 additions & 5 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class Form implements \IteratorAggregate, FormInterface
/**
* Whether the data in model, normalized and view format is
* synchronized. Data may not be synchronized if transformation errors
* occur.
* @var bool
* occur. This field stores cause of synchronization failure.
* @var TransformationFailedException
*/
private $synchronized = true;
private $synchronizationFailureCause = null;

/**
* Whether the form's data has been initialized.
Expand Down Expand Up @@ -634,7 +634,7 @@ public function submit($submittedData, $clearMissing = true)
$viewData = $this->normToView($normData);
}
} catch (TransformationFailedException $e) {
$this->synchronized = false;
$this->synchronizationFailureCause = $e;

// If $viewData was not yet set, set it to $submittedData so that
// the erroneous data is accessible on the form.
Expand Down Expand Up @@ -711,7 +711,17 @@ public function isBound()
*/
public function isSynchronized()
{
return $this->synchronized;
return null === $this->synchronizationFailureCause;
}

/**
* @return TransformationFailedException
Copy link
Member

Choose a reason for hiding this comment

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

TransformationFailedException|null

*
* @internal Should not be called by user code.
*/
public function getSynchronizationFailureCause()
{
return $this->synchronizationFailureCause;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ public function testCollectSubmittedDataCountsErrors()
$form1 = $this->createForm('form1');
$childForm1 = $this->createForm('child1');
$form2 = $this->createForm('form2');
$form3 = $this->createForm('form3');

$form1->add($childForm1);

Expand All @@ -510,6 +511,10 @@ public function testCollectSubmittedDataCountsErrors()
->method('extractSubmittedData')
->with($form2)
->will($this->returnValue(array('errors' => array('baz'))));
$this->dataExtractor->expects($this->at(3))
->method('extractSubmittedData')
->with($form3)
->will($this->returnValue(array('errors' => array(), 'synchronization_failure_cause' => 'Failure!')));

$this->dataCollector->collectSubmittedData($form1);

Expand All @@ -521,6 +526,10 @@ public function testCollectSubmittedDataCountsErrors()
$data = $this->dataCollector->getData();
$this->assertSame(4, $data['nb_errors']);

$this->dataCollector->collectSubmittedData($form3);

$data = $this->dataCollector->getData();
$this->assertSame(5, $data['nb_errors']);
}

private function createForm($name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,19 @@ function () {

$form->submit('Foobar');

$submittedData = $this->dataExtractor->extractSubmittedData($form);

$this->assertSynchronizationFailureCause('Fail!', $submittedData);
unset($submittedData['synchronization_failure_cause']);

$this->assertSame(array(
'submitted_data' => array(
'norm' => "'Foobar'",
'model' => 'NULL',
),
'errors' => array(),
'synchronized' => 'false',
), $this->dataExtractor->extractSubmittedData($form));
), $submittedData);
}

public function testExtractViewVariables()
Expand Down Expand Up @@ -424,4 +429,13 @@ private function createBuilder($name, array $options = array())
{
return new FormBuilder($name, null, $this->dispatcher, $this->factory, $options);
}

private function assertSynchronizationFailureCause($expectedCause, $submittedData)
{
$this->assertTrue(
isset($submittedData['synchronization_failure_cause']),
'cause of synchronization failure was not be extracted'
);
$this->assertContains($expectedCause, $submittedData['synchronization_failure_cause']);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ public function testNotSynchronizedIfViewReverseTransformationFailed()
$form->submit('foobar');

$this->assertFalse($form->isSynchronized());
$this->assertInstanceOf(
'Symfony\\Component\\Form\\Exception\\TransformationFailedException',
$form->getSynchronizationFailureCause()
);
}

public function testNotSynchronizedIfModelReverseTransformationFailed()
Expand Down