bug #10591 [Form] Buttons are now disabled if their containing form is disabled (webmozart)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] Buttons are now disabled if their containing form is disabled

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10109
| License       | MIT
| Doc PR        | -

Commits
-------

ebfee72 [Form] Added test for disabling buttons
6bb355e [Form] Added check for parent disabled status in Button form elements
This commit is contained in:
Fabien Potencier 2014-03-31 12:57:57 +02:00
commit 0a6a1c44c0
4 changed files with 98 additions and 22 deletions

View File

@ -321,7 +321,11 @@ class Button implements \IteratorAggregate, FormInterface
*/
public function isDisabled()
{
return $this->config->getDisabled();
if (null === $this->parent || !$this->parent->isDisabled()) {
return $this->config->getDisabled();
}
return true;
}
/**

View File

@ -252,6 +252,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
@ -260,6 +262,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
@ -286,6 +290,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setDisabled($disabled)
{
$this->disabled = $disabled;
return $this;
}
/**
@ -410,6 +416,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setType(ResolvedFormTypeInterface $type)
{
$this->type = $type;
return $this;
}
/**

View File

@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests;
use Symfony\Component\Form\ButtonBuilder;
use Symfony\Component\Form\FormBuilder;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ButtonTest extends \PHPUnit_Framework_TestCase
{
private $dispatcher;
private $factory;
protected function setUp()
{
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
}
/**
* @dataProvider getDisabledStates
*/
public function testDisabledIfParentIsDisabled($parentDisabled, $buttonDisabled, $result)
{
$form = $this->getFormBuilder('form')
->setDisabled($parentDisabled)
->getForm();
$button = $this->getButtonBuilder('button')
->setDisabled($buttonDisabled)
->getForm();
$button->setParent($form);
$this->assertSame($result, $button->isDisabled());
}
public function getDisabledStates()
{
return array(
// parent, button, result
array(true, true, true),
array(true, false, true),
array(false, true, true),
array(false, false, false),
);
}
private function getButtonBuilder($name)
{
return new ButtonBuilder($name);
}
private function getFormBuilder($name)
{
return new FormBuilder($name, null, $this->dispatcher, $this->factory);
}
}

View File

@ -170,34 +170,28 @@ class SimpleFormTest extends AbstractFormTest
$this->assertFalse($child->isRequired());
}
public function testAlwaysDisabledIfParentDisabled()
/**
* @dataProvider getDisabledStates
*/
public function testAlwaysDisabledIfParentDisabled($parentDisabled, $disabled, $result)
{
$parent = $this->getBuilder()->setDisabled(true)->getForm();
$child = $this->getBuilder()->setDisabled(false)->getForm();
$parent = $this->getBuilder()->setDisabled($parentDisabled)->getForm();
$child = $this->getBuilder()->setDisabled($disabled)->getForm();
$child->setParent($parent);
$this->assertTrue($child->isDisabled());
$this->assertSame($result, $child->isDisabled());
}
public function testDisabled()
public function getDisabledStates()
{
$parent = $this->getBuilder()->setDisabled(false)->getForm();
$child = $this->getBuilder()->setDisabled(true)->getForm();
$child->setParent($parent);
$this->assertTrue($child->isDisabled());
}
public function testNotDisabled()
{
$parent = $this->getBuilder()->setDisabled(false)->getForm();
$child = $this->getBuilder()->setDisabled(false)->getForm();
$child->setParent($parent);
$this->assertFalse($child->isDisabled());
return array(
// parent, button, result
array(true, true, true),
array(true, false, true),
array(false, true, true),
array(false, false, false),
);
}
public function testGetRootReturnsRootOfParent()