Skip to content

[Form] Fixed TransformationFailedExceptions thrown by model transformers to be caught by the form #4860

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

Merged
merged 2 commits into from
Jul 11, 2012
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ CHANGELOG
consumed by HTML5 browsers, if the widget is "single_text"
* deprecated the options "data_timezone" and "user_timezone" in DateType, DateTimeType and TimeType
and renamed them to "model_timezone" and "view_timezone"
* fixed: TransformationFailedExceptions thrown in the model transformer are now caught by the form
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ public function guessTypeForConstraint(Constraint $constraint)

case 'Symfony\Component\Validator\Constraints\Url':
return new TypeGuess('url', array(), Guess::HIGH_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\True':
case 'Symfony\Component\Validator\Constraints\False':
return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
}

return null;
}

/**
Expand All @@ -186,8 +192,11 @@ public function guessRequiredForConstraint(Constraint $constraint)
switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\NotNull':
case 'Symfony\Component\Validator\Constraints\NotBlank':
case 'Symfony\Component\Validator\Constraints\True':
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
}

return null;
}

/**
Expand Down Expand Up @@ -215,6 +224,8 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
}

return null;
}

/**
Expand Down Expand Up @@ -250,6 +261,8 @@ public function guessPatternForConstraint(Constraint $constraint)
}
break;
}

return null;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,7 @@ public function bind($submittedData)
try {
// Normalize data to unified representation
$normData = $this->viewToNorm($viewData);
$synchronized = true;
} catch (TransformationFailedException $e) {
}

if ($synchronized) {
// Hook to change content of the data into the normalized
// representation
$event = new FormEvent($this, $normData);
Expand All @@ -560,6 +556,9 @@ public function bind($submittedData)
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
$viewData = $this->normToView($normData);

$synchronized = true;
} catch (TransformationFailedException $e) {
}

$this->bound = true;
Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public function testSynchronizedAfterBinding()
$this->assertTrue($this->form->isSynchronized());
}

public function testNotSynchronizedIfTransformationFailed()
public function testNotSynchronizedIfViewReverseTransformationFailed()
{
$transformer = $this->getDataTransformer();
$transformer->expects($this->once())
Expand All @@ -496,6 +496,22 @@ public function testNotSynchronizedIfTransformationFailed()
$this->assertFalse($form->isSynchronized());
}

public function testNotSynchronizedIfModelReverseTransformationFailed()
{
$transformer = $this->getDataTransformer();
$transformer->expects($this->once())
->method('reverseTransform')
->will($this->throwException(new TransformationFailedException()));

$form = $this->getBuilder()
->addModelTransformer($transformer)
->getForm();

$form->bind('foobar');

$this->assertFalse($form->isSynchronized());
}

public function testEmptyDataCreatedBeforeTransforming()
{
$form = $this->getBuilder()
Expand Down