Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 19d8510

Browse filesBrowse files
committed
[Form] Improved Form::add() and FormBuilder::add() to accept integers as field names
1 parent fb71964 commit 19d8510
Copy full SHA for 19d8510

11 files changed

+65
-28
lines changed

‎src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function preSetData(FormEvent $event)
8383

8484
// Then add all rows again in the correct order
8585
foreach ($data as $name => $value) {
86-
$form->add((string) $name, $this->type, array_replace(array(
86+
$form->add($name, $this->type, array_replace(array(
8787
'property_path' => '['.$name.']',
8888
), $this->options));
8989
}
@@ -115,7 +115,7 @@ public function preBind(FormEvent $event)
115115
if ($this->allowAdd) {
116116
foreach ($data as $name => $value) {
117117
if (!$form->has($name)) {
118-
$form->add((string) $name, $this->type, array_replace(array(
118+
$form->add($name, $this->type, array_replace(array(
119119
'property_path' => '['.$name.']',
120120
), $this->options));
121121
}

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private function addSubForms(FormBuilderInterface $builder, array $choiceViews,
256256
$choiceType = 'radio';
257257
}
258258

259-
$builder->add((string) $i, $choiceType, $choiceOpts);
259+
$builder->add($i, $choiceType, $choiceOpts);
260260
}
261261
}
262262
}

‎src/Symfony/Component/Form/Form.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Form.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,8 @@ public function add($child, $type = null, array $options = array())
890890
}
891891

892892
if (!$child instanceof FormInterface) {
893-
if (!is_string($child)) {
894-
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface');
893+
if (!is_string($child) && !is_int($child)) {
894+
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
895895
}
896896

897897
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {

‎src/Symfony/Component/Form/FormBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormBuilder.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public function add($child, $type = null, array $options = array())
7878
return $this;
7979
}
8080

81-
if (!is_string($child)) {
82-
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilder');
81+
if (!is_string($child) && !is_int($child)) {
82+
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormBuilder');
8383
}
8484

8585
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {

‎src/Symfony/Component/Form/FormBuilderInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormBuilderInterface.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuild
2323
* If you add a nested group, this group should also be represented in the
2424
* object hierarchy.
2525
*
26-
* @param string|FormBuilderInterface $child
27-
* @param string|FormTypeInterface $type
28-
* @param array $options
26+
* @param string|integer|FormBuilderInterface $child
27+
* @param string|FormTypeInterface $type
28+
* @param array $options
2929
*
3030
* @return FormBuilderInterface The builder object.
3131
*/

‎src/Symfony/Component/Form/FormConfigBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormConfigBuilder.php
+7-9Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
144144
/**
145145
* Creates an empty form configuration.
146146
*
147-
* @param string $name The form name
147+
* @param string|integer $name The form name
148148
* @param string $dataClass The class of the form's data
149149
* @param EventDispatcherInterface $dispatcher The event dispatcher
150150
* @param array $options The form options
@@ -154,15 +154,13 @@ class FormConfigBuilder implements FormConfigBuilderInterface
154154
*/
155155
public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, array $options = array())
156156
{
157-
$name = (string) $name;
158-
159157
self::validateName($name);
160158

161159
if (null !== $dataClass && !class_exists($dataClass)) {
162160
throw new \InvalidArgumentException(sprintf('The data class "%s" is not a valid class.', $dataClass));
163161
}
164162

165-
$this->name = $name;
163+
$this->name = (string) $name;
166164
$this->dataClass = $dataClass;
167165
$this->dispatcher = $dispatcher;
168166
$this->options = $options;
@@ -895,15 +893,15 @@ public function getFormConfig()
895893
/**
896894
* Validates whether the given variable is a valid form name.
897895
*
898-
* @param string $name The tested form name.
896+
* @param string|integer $name The tested form name.
899897
*
900-
* @throws UnexpectedTypeException If the name is not a string.
898+
* @throws UnexpectedTypeException If the name is not a string or an integer.
901899
* @throws \InvalidArgumentException If the name contains invalid characters.
902900
*/
903901
public static function validateName($name)
904902
{
905-
if (!is_string($name)) {
906-
throw new UnexpectedTypeException($name, 'string');
903+
if (null !== $name && !is_string($name) && !is_int($name)) {
904+
throw new UnexpectedTypeException($name, 'string, integer or null');
907905
}
908906

909907
if (!self::isValidName($name)) {
@@ -930,6 +928,6 @@ public static function validateName($name)
930928
*/
931929
public static function isValidName($name)
932930
{
933-
return '' === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
931+
return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
934932
}
935933
}

‎src/Symfony/Component/Form/FormFactoryInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormFactoryInterface.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function create($type = 'form', $data = null, array $options = array(), F
3737
*
3838
* @see createNamedBuilder()
3939
*
40-
* @param string $name The name of the form
40+
* @param string|integer $name The name of the form
4141
* @param string|FormTypeInterface $type The type of the form
4242
* @param mixed $data The initial data
4343
* @param array $options The options
@@ -83,7 +83,7 @@ public function createBuilder($type = 'form', $data = null, array $options = arr
8383
/**
8484
* Returns a form builder.
8585
*
86-
* @param string $name The name of the form
86+
* @param string|integer $name The name of the form
8787
* @param string|FormTypeInterface $type The type of the form
8888
* @param mixed $data The initial data
8989
* @param array $options The options

‎src/Symfony/Component/Form/FormInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormInterface.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public function getParent();
4141
/**
4242
* Adds a child to the form.
4343
*
44-
* @param FormInterface|string $child The FormInterface instance or the name of the child.
45-
* @param string|null $type The child's type, if a name was passed.
46-
* @param array $options The child's options, if a name was passed.
44+
* @param FormInterface|string|integer $child The FormInterface instance or the name of the child.
45+
* @param string|null $type The child's type, if a name was passed.
46+
* @param array $options The child's options, if a name was passed.
4747
*
4848
* @return FormInterface The form instance
4949
*

‎src/Symfony/Component/Form/Tests/CompoundFormTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/CompoundFormTest.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ public function testAddUsingNameAndType()
156156
$this->assertSame(array('foo' => $child), $this->form->all());
157157
}
158158

159+
public function testAddUsingIntegerNameAndType()
160+
{
161+
$child = $this->getBuilder(0)->getForm();
162+
163+
$this->factory->expects($this->once())
164+
->method('createNamed')
165+
->with('0', 'text', null, array('bar' => 'baz'))
166+
->will($this->returnValue($child));
167+
168+
// in order to make casting unnecessary
169+
$this->form->add(0, 'text', array('bar' => 'baz'));
170+
171+
$this->assertTrue($this->form->has(0));
172+
$this->assertSame($this->form, $child->getParent());
173+
$this->assertSame(array(0 => $child), $this->form->all());
174+
}
175+
159176
public function testAddUsingNameButNoType()
160177
{
161178
$this->form = $this->getBuilder('name', null, '\stdClass')

‎src/Symfony/Component/Form/Tests/FormBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/FormBuilderTest.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function testNoSetName()
5050
$this->assertFalse(method_exists($this->builder, 'setName'));
5151
}
5252

53-
public function testAddNameNoString()
53+
public function testAddNameNoStringAndNoInteger()
5454
{
5555
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
56-
$this->builder->add(1234);
56+
$this->builder->add(true);
5757
}
5858

5959
public function testAddTypeNoString()
@@ -82,6 +82,13 @@ public function testAdd()
8282
$this->assertTrue($this->builder->has('foo'));
8383
}
8484

85+
public function testAddIntegerName()
86+
{
87+
$this->assertFalse($this->builder->has(0));
88+
$this->builder->add(0, 'text');
89+
$this->assertTrue($this->builder->has(0));
90+
}
91+
8592
public function testAll()
8693
{
8794
$this->factory->expects($this->once())

‎src/Symfony/Component/Form/Tests/FormConfigTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/FormConfigTest.php
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Form\Tests;
1313

14+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
1416
/**
1517
* @author Bernhard Schussek <bschussek@gmail.com>
1618
*/
@@ -21,8 +23,6 @@ class FormConfigTest extends \PHPUnit_Framework_TestCase
2123
public function getHtml4Ids()
2224
{
2325
return array(
24-
array('a0', true),
25-
array('a9', true),
2626
array('z0', true),
2727
array('A0', true),
2828
array('A9', true),
@@ -53,6 +53,16 @@ public function getHtml4Ids()
5353
// For root forms, leading underscores will be stripped from the
5454
// "id" attribute to produce valid HTML4.
5555
array('_', true),
56+
// Integers are allowed
57+
array(0, true),
58+
array(123, true),
59+
// NULL is allowed
60+
array(null, true),
61+
// Other types are not
62+
array(1.23, false),
63+
array(5., false),
64+
array(true, false),
65+
array(new \stdClass(), false),
5666
);
5767
}
5868

@@ -68,6 +78,11 @@ public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
6878
if (!$accepted) {
6979
$this->fail(sprintf('The value "%s" should not be accepted', $name));
7080
}
81+
} catch (UnexpectedTypeException $e) {
82+
// if the value was not accepted, but should be, rethrow exception
83+
if ($accepted) {
84+
throw $e;
85+
}
7186
} catch (\InvalidArgumentException $e) {
7287
// if the value was not accepted, but should be, rethrow exception
7388
if ($accepted) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.