merged branch bschussek/issue4852 (PR #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: #4852
Todo: -
This commit is contained in:
Fabien Potencier 2012-07-11 16:53:31 +02:00
commit 83c058fbf1
4 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -171,7 +171,13 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
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;
}
/**
@ -186,8 +192,11 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
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;
}
/**
@ -215,6 +224,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
}
return null;
}
/**
@ -250,6 +261,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
}
break;
}
return null;
}
/**

View File

@ -544,11 +544,7 @@ class Form implements \IteratorAggregate, FormInterface
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);
@ -560,6 +556,9 @@ class Form implements \IteratorAggregate, FormInterface
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
$viewData = $this->normToView($normData);
$synchronized = true;
} catch (TransformationFailedException $e) {
}
$this->bound = true;

View File

@ -480,7 +480,7 @@ class SimpleFormTest extends AbstractFormTest
$this->assertTrue($this->form->isSynchronized());
}
public function testNotSynchronizedIfTransformationFailed()
public function testNotSynchronizedIfViewReverseTransformationFailed()
{
$transformer = $this->getDataTransformer();
$transformer->expects($this->once())
@ -496,6 +496,22 @@ class SimpleFormTest extends AbstractFormTest
$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()