10000 [Form] Fixed read_only attribute for expanded fields by helmer · Pull Request #3258 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed read_only attribute for expanded fields #3258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,23 @@ private function addSubFields(FormBuilder $builder, array $choiceViews, array $o
if (is_array($choiceView)) {
// Flatten groups
$this->addSubFields($builder, $choiceView, $options);
} elseif ($options['multiple']) {
$builder->add((string) $i, 'checkbox', array(
'value' => $choiceView->getValue(),
'label' => $choiceView->getLabel(),
// The user can check 0 or more checkboxes. If required
// is true, he is required to check all of them.
'required' => false,
'translation_domain' => $options['translation_domain'],
));
} else {
$builder->add((string) $i, 'radio', array(
$choiceOpts = array(
'value' => $choiceView->getValue(),
'label' => $choiceView->getLabel(),
'translation_domain' => $options['translation_domain'],
));
);

if ($options['multiple']) {
$choiceType = 'checkbox';
// The user can check 0 or more checkboxes. If required
// is true, he is required to check all of them.
$choiceOpts['required'] = false;
} else {
$choiceType = 'radio';
}

$builder->add((string) $i, $choiceType, $choiceOpts);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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'))
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
0