8000 merged branch helmer/readonly_fix (PR #3258) · symfony/symfony@5251177 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5251177

Browse files
committed
merged branch helmer/readonly_fix (PR #3258)
Commits ------- 8321593 [Form] DRYed ChoiceType 0753cee [Form] Fixed read_only attribute for expanded fields Discussion ---------- [Form] Fixed read_only attribute for expanded fields Expanded choice widgets lose the knowledge of read_only attribute value. Fixes bug introduced by #3193 --------------------------------------------------------------------------- by helmer at 2012-02-02T16:24:50Z Please hold before merging, @bschussek had some thoughts about my changes in ``ChoiceType``, waiting for feedback. --------------------------------------------------------------------------- by bschussek at 2012-02-02T16:33:12Z I'm fine with the refactoring then, but please split it into two commits at least. The changes in ChoiceType have nothing in common with the actual issue here. --------------------------------------------------------------------------- by helmer at 2012-02-02T19:40:39Z Tests added. --------------------------------------------------------------------------- by bschussek at 2012-02-03T10:14:32Z Great, thanks. @fabpot 👍
2 parents ffef177 + 8321593 commit 5251177

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,23 @@ private function addSubFields(FormBuilder $builder, array $choiceViews, array $o
190190
if (is_array($choiceView)) {
191191
// Flatten groups
192192
$this->addSubFields($builder, $choiceView, $options);
193-
} elseif ($options['multiple']) {
194-
$builder->add((string) $i, 'checkbox', array(
195-
'value' => $choiceView->getValue(),
196-
'label' => $choiceView->getLabel(),
197-
// The user can check 0 or more checkboxes. If required
198-
// is true, he is required to check all of them.
199-
'required' => false,
200-
'translation_domain' => $options['translation_domain'],
201-
));
202193
} else {
203-
$builder->add((string) $i, 'radio', array(
194+
$choiceOpts = array(
204195
'value' => $choiceView->getValue(),
205196
'label' => $choiceView->getLabel(),
206197
'translation_domain' => $options['translation_domain'],
207-
));
198+
);
199+
200+
if ($options['multiple']) {
201+
$choiceType = 'checkbox';
202+
// The user can check 0 or more checkboxes. If required
203+
// is true, he is required to check all of them.
204+
$choiceOpts['required'] = false;
205+
} else {
206+
$choiceType = 'radio';
207+
}
208+
209+
$builder->add((string) $i, $choiceType, $choiceOpts);
208210
}
209211
}
210212
}

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