Skip to content

Commit

Permalink
bug #10591 [Form] Buttons are now disabled if their containing form i…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
fabpot committed Mar 31, 2014
2 parents c778b03 + ebfee72 commit 0a6a1c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/Button.php
Expand Up @@ -321,7 +321,11 @@ public function isRequired()
*/
public function isDisabled()
{
return $this->config->getDisabled();
if (null === $this->parent || !$this->parent->isDisabled()) {
return $this->config->getDisabled();
}

return true;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/ButtonBuilder.php
Expand Up @@ -252,6 +252,8 @@ public function resetModelTransformers()
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

/**
Expand All @@ -260,6 +262,8 @@ public function setAttribute($name, $value)
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;

return $this;
}

/**
Expand All @@ -286,6 +290,8 @@ public function setDataMapper(DataMapperInterface $dataMapper = null)
public function setDisabled($disabled)
{
$this->disabled = $disabled;

return $this;
}

/**
Expand Down Expand Up @@ -410,6 +416,8 @@ public function setCompound($compound)
public function setType(ResolvedFormTypeInterface $type)
{
$this->type = $type;

return $this;
}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/Symfony/Component/Form/Tests/ButtonTest.php
@@ -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);
}
}
36 changes: 15 additions & 21 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Expand Up @@ -170,34 +170,28 @@ public function testNotRequired()
$this->assertFalse($child->isRequired());
}

public function testAlwaysDisabledIfParentDisabled()
{
$parent = $this->getBuilder()->setDisabled(true)->getForm();
$child = $this->getBuilder()->setDisabled(false)->getForm();

$child->setParent($parent);

$this->assertTrue($child->isDisabled());
}

public function testDisabled()
/**
* @dataProvider getDisabledStates
*/
public function testAlwaysDisabledIfParentDisabled($parentDisabled, $disabled, $result)
{
$parent = $this->getBuilder()->setDisabled(false)->getForm();
$child = $this->getBuilder()->setDisabled(true)->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 testNotDisabled()
public function getDisabledStates()
{
$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()
Expand Down

0 comments on commit 0a6a1c4

Please sign in to comment.