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

This commit is contained in:
Romain Geissler 2012-05-14 17:29:48 +02:00
parent 72b2f6984c
commit 47605f63e3
2 changed files with 22 additions and 2 deletions

View File

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

View File

@ -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);
}
}