merged branch helmer/readonly_fix (PR #3258)
Commits -------8321593
[Form] DRYed ChoiceType0753cee
[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 👍
This commit is contained in:
commit
5251177002
@ -190,21 +190,23 @@ class ChoiceType extends AbstractType
|
|||||||
if (is_array($choiceView)) {
|
if (is_array($choiceView)) {
|
||||||
// Flatten groups
|
// Flatten groups
|
||||||
$this->addSubFields($builder, $choiceView, $options);
|
$this->addSubFields($builder, $choiceView, $options);
|
||||||
} elseif ($options['multiple']) {
|
} else {
|
||||||
$builder->add((string) $i, 'checkbox', array(
|
$choiceOpts = array(
|
||||||
'value' => $choiceView->getValue(),
|
'value' => $choiceView->getValue(),
|
||||||
'label' => $choiceView->getLabel(),
|
'label' => $choiceView->getLabel(),
|
||||||
|
'translation_domain' => $options['translation_domain'],
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($options['multiple']) {
|
||||||
|
$choiceType = 'checkbox';
|
||||||
// The user can check 0 or more checkboxes. If required
|
// The user can check 0 or more checkboxes. If required
|
||||||
// is true, he is required to check all of them.
|
// is true, he is required to check all of them.
|
||||||
'required' => false,
|
$choiceOpts['required'] = false;
|
||||||
'translation_domain' => $options['translation_domain'],
|
} else {
|
||||||
));
|
$choiceType = 'radio';
|
||||||
} else {
|
}
|
||||||
$builder->add((string) $i, 'radio', array(
|
|
||||||
'value' => $choiceView->getValue(),
|
$builder->add((string) $i, $choiceType, $choiceOpts);
|
||||||
'label' => $choiceView->getLabel(),
|
|
||||||
'translation_domain' => $options['translation_domain'],
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,7 @@ class FieldType extends AbstractType
|
|||||||
public function buildView(FormView $view, FormInterface $form)
|
public function buildView(FormView $view, FormInterface $form)
|
||||||
{
|
{
|
||||||
$name = $form->getName();
|
$name = $form->getName();
|
||||||
|
$readOnly = $form->getAttribute('read_only');
|
||||||
|
|
||||||
if ($view->hasParent()) {
|
if ($view->hasParent()) {
|
||||||
if ('' === $name) {
|
if ('' === $name) {
|
||||||
@ -86,6 +87,9 @@ class FieldType extends AbstractType
|
|||||||
$id = $name;
|
$id = $name;
|
||||||
$fullName = $name;
|
$fullName = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Complex fields are read-only if themselves or their parent is.
|
||||||
|
$readOnly = $readOnly || $view->getParent()->get('read_only');
|
||||||
} else {
|
} else {
|
||||||
$id = $name;
|
$id = $name;
|
||||||
$fullName = $name;
|
$fullName = $name;
|
||||||
@ -106,9 +110,9 @@ class FieldType extends AbstractType
|
|||||||
->set('id', $id)
|
->set('id', $id)
|
||||||
->set('name', $name)
|
->set('name', $name)
|
||||||
->set('full_name', $fullName)
|
->set('full_name', $fullName)
|
||||||
|
->set('read_only', $readOnly)
|
||||||
->set('errors', $form->getErrors())
|
->set('errors', $form->getErrors())
|
||||||
->set('value', $form->getClientData())
|
->set('value', $form->getClientData())
|
||||||
->set('read_only', $form->getAttribute('read_only'))
|
|
||||||
->set('disabled', $form->isDisabled())
|
->set('disabled', $form->isDisabled())
|
||||||
->set('required', $form->isRequired())
|
->set('required', $form->isRequired())
|
||||||
->set('max_length', $form->getAttribute('max_length'))
|
->set('max_length', $form->getAttribute('max_length'))
|
||||||
|
@ -184,7 +184,7 @@ class FormBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether the form is disabled
|
* Set whether the form is disabled.
|
||||||
*
|
*
|
||||||
* @param Boolean $disabled Whether the form is disabled
|
* @param Boolean $disabled Whether the form is disabled
|
||||||
*
|
*
|
||||||
|
@ -150,6 +150,33 @@ class FieldTypeTest extends TypeTestCase
|
|||||||
$this->assertEquals('parent[child][grand_child]', $view['child']['grand_child']->get('full_name'));
|
$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()
|
public function testPassMaxLengthToView()
|
||||||
{
|
{
|
||||||
$form = $this->factory->create('field', null, array('max_length' => 10));
|
$form = $this->factory->create('field', null, array('max_length' => 10));
|
||||||
|
Reference in New Issue
Block a user