From 47605f63e3536b71ec008617c5adb39bb9a6dbfe Mon Sep 17 00:00:00 2001 From: Romain Geissler Date: Mon, 14 May 2012 17:29:48 +0200 Subject: [PATCH] [Form][DataMapper] Do not update form to data when form is read only --- .../Core/DataMapper/PropertyPathMapper.php | 2 +- .../DataMapper/PropertyPathMapperTest.php | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php index 6652c3843e..e2211007c6 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php @@ -77,7 +77,7 @@ class PropertyPathMapper implements DataMapperInterface public function mapFormToData(FormInterface $form, &$data) { - if ($form->getAttribute('property_path') !== null && $form->isSynchronized()) { + if ($form->getAttribute('property_path') !== null && $form->isSynchronized() && !$form->isReadOnly()) { $propertyPath = $form->getAttribute('property_path'); // If the data is identical to the value in $data, we are diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/DataMapper/PropertyPathMapperTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/DataMapper/PropertyPathMapperTest.php index af0b76ee7a..ce16702723 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/DataMapper/PropertyPathMapperTest.php @@ -34,7 +34,7 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase $this->propertyPath = null; } - private function getForm(PropertyPath $propertyPath = null) + private function getForm(PropertyPath $propertyPath = null, $synchronized = true, $readOnly = false) { $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface'); @@ -43,6 +43,14 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase ->with('property_path') ->will($this->returnValue($propertyPath)); + $form->expects($this->any()) + ->method('isSynchronized') + ->will($this->returnValue($synchronized)); + + $form->expects($this->any()) + ->method('isReadOnly') + ->will($this->returnValue($readOnly)); + return $form; } @@ -87,4 +95,16 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase $this->mapper->mapDataToForm(null, $form); } + + public function testMapFormToDataIgnoresReadOnlyForm() + { + $data = new \stdClass(); + + $form = $this->getForm($this->propertyPath, true, true); + + $this->propertyPath->expects($this->never()) + ->method('setValue'); + + $this->mapper->mapFormToData($form, $data); + } }