8000 merged branch bschussek/issue4852 (PR #4860) · jacobmaster/symfony@83c058f · GitHub
[go: up one dir, main page]

Skip to content

Commit 83c058f

Browse files
committed
merged branch bschussek/issue4852 (PR symfony#4860)
Commits ------- c919b81 [Form] Fixed TransformationFailedExceptions to be caught in the model transformers f06203a [Form] Improved ValidatorTypeGuesser to interpret the constraints True and False Discussion ---------- [Form] Fixed TransformationFailedExceptions thrown by model transformers to be caught by the form Bug fix: yes Feature addition: (yes) Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: symfony#4852 Todo: -
2 parents c3b79f1 + c919b81 commit 83c058f

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,4 @@ CHANGELOG
156156
consumed by HTML5 browsers, if the widget is "single_text"
157157
* deprecated the options "data_timezone" and "user_timezone" in DateType, DateTimeType and TimeType
158158
and renamed them to "model_timezone" and "view_timezone"
159+
* fixed: TransformationFailedExceptions thrown in the model transformer are now caught by the form

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ public function guessTypeForConstraint(Constraint $constraint)
171171

172172
case 'Symfony\Component\Validator\Constraints\Url':
173173
return new TypeGuess('url', array(), Guess::HIGH_CONFIDENCE);
174+
175+
case 'Symfony\Component\Validator\Constraints\True':
176+
case 'Symfony\Component\Validator\Constraints\False':
177+
return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
174178
}
179+
180+
return null;
175181
}
176182

177183
/**
@@ -186,8 +192,11 @@ public function guessRequiredForConstraint(Constraint $constraint)
186192
switch (get_class($constraint)) {
187193
case 'Symfony\Component\Validator\Constraints\NotNull':
188194
case 'Symfony\Component\Validator\Constraints\NotBlank':
195+
case 'Symfony\Component\Validator\Constraints\True':
189196
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
190197
}
198+
199+
return null;
191200
}
192201

193202
/**
@@ -215,6 +224,8 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
215224
case 'Symfony\Component\Validator\Constraints\Size':
216225
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
217226
}
227+
228+
return null;
218229
}
219230

220231
/**
@@ -250,6 +261,8 @@ public function guessPatternForConstraint(Constraint $constraint)
250261
}
251262
break;
252263
}
264+
265+
return null;
253266
}
254267

255268
/**

src/Symfony/Component/Form/Form.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,7 @@ public function bind($submittedData)
544544
try {
545545
// Normalize data to unified representation
546546
$normData = $this->viewToNorm($viewData);
547-
$synchronized = true;
548-
} catch (TransformationFailedException $e) {
549-
}
550547

551-
if ($synchronized) {
552548
// Hook to change content of the data into the normalized
553549
// representation
554550
$event = new FormEvent($this, $normData);
@@ -560,6 +556,9 @@ public function bind($submittedData)
560556
// Synchronize representations - must not change the content!
561557
$modelData = $this->normToModel($normData);
562558
$viewData = $this->normToView($normData);
559+
560+
$synchronized = true;
561+
} catch (TransformationFailedException $e) {
563562
}
564563

565564
$this->bound = true;

src/Symfony/Component/Form/Tests/SimpleFormTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public function testSynchronizedAfterBinding()
480480
$this->assertTrue($this->form->isSynchronized());
481481
}
482482

483-
public function testNotSynchronizedIfTransformationFailed()
483+
public function testNotSynchronizedIfViewReverseTransformationFailed()
484484
{
485485
$transformer = $this->getDataTransformer();
486486
$transformer->expects($this->once())
@@ -496,6 +496,22 @@ public function testNotSynchronizedIfTransformationFailed()
496496
$this->assertFalse($form->isSynchronized());
497497
}
498498

499+
public function testNotSynchronizedIfModelReverseTransformationFailed()
500+
{
501+
$transformer = $this->getDataTransformer();
502+
$transformer->expects($this->once())
503+
->method('reverseTransform')
504+
->will($this->throwException(new TransformationFailedException()));
505+
506+
$form = $this->getBuilder()
507+
->addModelTransformer($transformer)
508+
->getForm();
509+
510+
$form->bind('foobar');
511+
512+
$this->assertFalse($form->isSynchronized());
513+
}
514+
499515
public function testEmptyDataCreatedBeforeTransforming()
500516
{
501517
$form = $this->getBuilder()

0 commit comments

Comments
 (0)
0