10000 [Form] Fixed read_only attribute for expanded fields · symfony/symfony@0753cee · GitHub
[go: up one dir, main page]

Skip to content

Commit 0753cee

Browse files
committed
[Form] Fixed read_only attribute for expanded fields
1 parent 0914a38 commit 0753cee

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function buildForm(FormBuilder $builder, array $options)
7373
public function buildView(FormView $view, FormInterface $form)
7474
{
7575
$name = $form->getName();
76+
$readOnly = $form->getAttribute('read_only');
7677

7778
if ($view->hasParent()) {
7879
if ('' === $name) {
@@ -86,6 +87,9 @@ public function buildView(FormView $view, FormInterface $form)
8687
$id = $name;
8788
$fullName = $name;
8889
}
90+
91+
// Complex fields are read-only if themselves or their parent is.
92+
$readOnly = $readOnly || $view->getParent()->get('read_only');
8993
} else {
9094
$id = $name;
9195
$fullName = $name;
@@ -106,9 +110,9 @@ public function buildView(FormView $view, FormInterface $form)
106110
->set('id', $id)
107111
->set('name', $name)
108112
->set('full_name', $fullName)
113+
->set('read_only', $readOnly)
109114
->set('errors', $form->getErrors())
110115
->set('value', $form->getClientData())
111-
->set('read_only', $form->getAttribute('read_only'))
112116
->set('disabled', $form->isDisabled())
113117
->set('required', $form->isRequired())
114118
->set('max_length', $form->getAttribute('max_length'))

src/Symfony/Component/Form/FormBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function getData()
184184
}
185185

186186
/**
187-
* Set whether the form is disabled
187+
* Set whether the form is disabled.
188188
*
189189
* @param Boolean $disabled Whether the form is disabled
190190
*

tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ public function testPassIdAndNameToViewWithGrandParent()
150150
$this->assertEquals('parent[child][grand_child]', $view['child']['grand_child']->get('full_name'));
151151
}
152152

153+
public function testNonReadOnlyFieldWithReadOnlyParentBeingReadOnly()
154+
{
155+
$parent = $this->factory->createNamed('field', 'parent', null, array('read_only' => true));
156+
$child = $this->factory->createNamed('field', 'child');
157+
$view = $parent->add($child)->createView();
158+
159+
$this->assertTrue($view['child']->get('read_only'));
160+
}
161+
162+
public function testReadOnlyFieldWithNonReadOnlyParentBeingReadOnly()
163+
{
164+
$parent = $this->factory->createNamed('field', 'parent');
165+
$child = $this->factory->createNamed('field', 'child', null, array('read_only' => true));
166+
$view = $parent->add($child)->createView();
167+
168+
$this->assertTrue($view['child']->get('read_only'));
169+
}
170+
171+
public function testNonReadOnlyFieldWithNonReadOnlyParentBeingNonReadOnly()
172+
{
173+
$parent = $this->factory->createNamed('field', 'parent');
174+
$child = $this->factory->createNamed('field', 'child');
175+
$view = $parent->add($child)->createView();
176+
177+
$this->assertFalse($view['child']->get('read_only'));
178+
}
179+
153180
public function testPassMaxLengthToView()
154181
{
155182
$form = $this->factory->create('field', null, array('max_length' => 10));

0 commit comments

Comments
 (0)
0