Преглед изворни кода

[Form][DataMapper] Do not update form to data when form is read only

Romain Geissler пре 13 година
родитељ
комит
47605f63e3

+ 1 - 1
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

+ 21 - 1
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);
+    }
 }