Skip to content

Commit 0309d2c

Browse files
committed
invalidate forms on transformation failures
1 parent 8322494 commit 0309d2c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,22 @@ public function submit($submittedData, $clearMissing = true)
664664
$dispatcher->dispatch(FormEvents::POST_SUBMIT, $event);
665665
}
666666

667+
if (null !== $this->transformationFailure && null === $this->config->getOption('invalid_message') && $this->isValid()) {
668+
$childrenSynchronized = true;
669+
670+
foreach ($this->children as $child) {
671+
if (!$child->isSynchronized()) {
672+
$childrenSynchronized = false;
673+
break;
674+
}
675+
}
676+
677+
if ($childrenSynchronized) {
678+
$clientDataAsString = is_scalar($this->getViewData()) ? (string) $this->getViewData() : \gettype($this->getViewData());
679+
$this->addError(new FormError('This value is not valid.', 'This value is not valid.', array('{{ value }}' => $clientDataAsString), null, $this->transformationFailure));
680+
}
681+
}
682+
667683
return $this;
668684
}
669685

src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,16 @@ function () {
385385

386386
$form->submit('Foobar');
387387

388+
$submittedData = $this->dataExtractor->extractSubmittedData($form);
389+
390+
$this->assertInternalType('array', $submittedData);
391+
$this->assertArrayHasKey('submitted_data', $submittedData);
388392
$this->assertSame(array(
389-
'submitted_data' => array(
390-
'norm' => "'Foobar'",
391-
'model' => 'NULL',
392-
),
393-
'errors' => array(),
394-
'synchronized' => 'false',
395-
), $this->dataExtractor->extractSubmittedData($form));
393+
'norm' => "'Foobar'",
394+
'model' => 'NULL',
395+
), $submittedData['submitted_data']);
396+
$this->assertArrayHasKey('synchronized', $submittedData);
397+
$this->assertSame('false', $submittedData['synchronized']);
396398
}
397399

398400
public function testExtractViewVariables()

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function testAbortMappingIfNotSynchronized()
182182
$this->mapper->mapViolation($violation, $parent);
183183

184184
$this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one');
185-
$this->assertCount(0, $child->getErrors(), $child->getName().' should not have an error, but has one');
185+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error as it is not synchronized');
186186
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName().' should not have an error, but has one');
187187
}
188188

@@ -206,7 +206,7 @@ public function testAbortDotRuleMappingIfNotSynchronized()
206206
$this->mapper->mapViolation($violation, $parent);
207207

208208
$this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one');
209-
$this->assertCount(0, $child->getErrors(), $child->getName().' should not have an error, but has one');
209+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error as it is not synchronized');
210210
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName().' should not have an error, but has one');
211211
}
212212

0 commit comments

Comments
 (0)