Permalink
Browse files

[Form] Fixed read_only attribute for expanded fields

  • Loading branch information...
1 parent 0914a38 commit 0753cee11a92c3b5415529ba17452b1b111a3914 @helmer helmer committed Feb 2, 2012
@@ -73,6 +73,7 @@ public function buildForm(FormBuilder $builder, array $options)
public function buildView(FormView $view, FormInterface $form)
{
$name = $form->getName();
+ $readOnly = $form->getAttribute('read_only');
if ($view->hasParent()) {
if ('' === $name) {
@@ -86,6 +87,9 @@ public function buildView(FormView $view, FormInterface $form)
$id = $name;
$fullName = $name;
}
+
+ // Complex fields are read-only if themselves or their parent is.
+ $readOnly = $readOnly || $view->getParent()->get('read_only');
} else {
$id = $name;
$fullName = $name;
@@ -106,9 +110,9 @@ public function buildView(FormView $view, FormInterface $form)
->set('id', $id)
->set('name', $name)
->set('full_name', $fullName)
+ ->set('read_only', $readOnly)
->set('errors', $form->getErrors())
->set('value', $form->getClientData())
- ->set('read_only', $form->getAttribute('read_only'))
->set('disabled', $form->isDisabled())
->set('required', $form->isRequired())
->set('max_length', $form->getAttribute('max_length'))
@@ -184,7 +184,7 @@ public function getData()
}
/**
- * Set whether the form is disabled
+ * Set whether the form is disabled.
*
* @param Boolean $disabled Whether the form is disabled
*
@@ -150,6 +150,33 @@ public function testPassIdAndNameToViewWithGrandParent()
$this->assertEquals('parent[child][grand_child]', $view['child']['grand_child']->get('full_name'));
}
+ public function testNonReadOnlyFieldWithReadOnlyParentBeingReadOnly()
+ {
+ $parent = $this->factory->createNamed('field', 'parent', null, array('read_only' => true));
+ $child = $this->factory->createNamed('field', 'child');
+ $view = $parent->add($child)->createView();
+
+ $this->assertTrue($view['child']->get('read_only'));
+ }
+
+ public function testReadOnlyFieldWithNonReadOnlyParentBeingReadOnly()
+ {
+ $parent = $this->factory->createNamed('field', 'parent');
+ $child = $this->factory->createNamed('field', 'child', null, array('read_only' => true));
+ $view = $parent->add($child)->createView();
+
+ $this->assertTrue($view['child']->get('read_only'));
+ }
+
+ public function testNonReadOnlyFieldWithNonReadOnlyParentBeingNonReadOnly()
+ {
+ $parent = $this->factory->createNamed('field', 'parent');
+ $child = $this->factory->createNamed('field', 'child');
+ $view = $parent->add($child)->createView();
+
+ $this->assertFalse($view['child']->get('read_only'));
+ }
+
public function testPassMaxLengthToView()
{
$form = $this->factory->create('field', null, array('max_length' => 10));

0 comments on commit 0753cee

Please sign in to comment.