Skip to content

Commit 41d6758

Browse files
committed
bug #19306 [Form] fixed bug - name in ButtonBuilder (cheprasov)
This PR was squashed before being merged into the 2.7 branch (closes #19306). Discussion ---------- [Form] fixed bug - name in ButtonBuilder | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? |no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - **Bug:** For any scalar of name, expression `empty($name) && 0 != $name` is never true, and as result - empty string ('') is allowed. **Examples:** ```php $name = ''; var_dump(empty($name) && 0 != $name); // false $name = '0'; var_dump(empty($name) && 0 != $name); // false $name = null; var_dump(empty($name) && 0 != $name); // false $name = false; var_dump(empty($name) && 0 != $name); // false $name = 0; var_dump(empty($name) && 0 != $name); // false ``` Commits ------- f507023 [Form] fixed bug - name in ButtonBuilder
2 parents 0259bed + f507023 commit 41d6758

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/Symfony/Component/Form/ButtonBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
6262
*/
6363
public function __construct($name, array $options = array())
6464
{
65-
if (empty($name) && 0 != $name) {
65+
$name = (string) $name;
66+
if ('' === $name) {
6667
throw new InvalidArgumentException('Buttons cannot have empty names.');
6768
}
6869

69-
$this->name = (string) $name;
70+
$this->name = $name;
7071
$this->options = $options;
7172
}
7273

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests;
13+
14+
use Symfony\Component\Form\ButtonBuilder;
15+
16+
/**
17+
* @author Alexander Cheprasov <[email protected]>
18+
*/
19+
class ButtonBuilderTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function getValidNames()
22+
{
23+
return array(
24+
array('reset'),
25+
array('submit'),
26+
array('foo'),
27+
array('0'),
28+
array(0),
29+
array('button[]'),
30+
);
31+
}
32+
33+
/**
34+
* @dataProvider getValidNames
35+
*/
36+
public function testValidNames($name)
37+
{
38+
$this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder($name));
39+
}
40+
41+
public function getInvalidNames()
42+
{
43+
return array(
44+
array(''),
45+
array(false),
46+
array(null),
47+
);
48+
}
49+
50+
/**
51+
* @dataProvider getInvalidNames
52+
*/
53+
public function testInvalidNames($name)
54+
{
55+
$this->setExpectedException(
56+
'\Symfony\Component\Form\Exception\InvalidArgumentException',
57+
'Buttons cannot have empty names.'
58+
);
59+
new ButtonBuilder($name);
60+
}
61+
}

0 commit comments

Comments
 (0)