8000 [Form] Improved Form::add() and FormBuilder::add() to accept integers… · waibo/symfony@19d8510 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19d8510

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

File tree

11 files changed

+65
-28
lines changed

11 files changed

+65
-28
lines changed

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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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< 8000 code class="diff-text syntax-highlighted-line">
{
161178
$this->form = $this->getBuilder('name', null, '\stdClass')

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

Lines 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())

0 commit comments

Comments
 (0)
0