8000 [Form] Added an alternative signature Form::add($name, $type, $options) by webmozart · Pull Request #6355 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Added an alternative signature Form::add($name, $type, $options) #6355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Form] Improved Form::add() and FormBuilder::add() to accept integers…
… as field names
  • Loading branch information
webmozart committed Dec 18, 2012
commit 19d8510288c8bcd0724027e7842599916b796890
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function preSetData(FormEvent $event)

// Then add all rows again in the correct order
foreach ($data as $name => $value) {
$form->add((string) $name, $this->type, array_replace(array(
$form->add($name, $this->type, array_replace(array(
'property_path' => '['.$name.']',
), $this->options));
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function preBind(FormEvent $event)
if ($this->allowAdd) {
foreach ($data as $name => $value) {
if (!$form->has($name)) {
$form->add((string) $name, $this->type, array_replace(array(
$form->add($name, $this->type, array_replace(array(
'property_path' => '['.$name.']',
), $this->options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private function addSubForms(FormBuilderInterface $builder, array $choiceViews,
$choiceType = 'radio';
}

$builder->add((string) $i, $choiceType, $choiceOpts);
$builder->add($i, $choiceType, $choiceOpts);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,8 @@ public function add($child, $type = null, array $options = array())
}

if (!$child instanceof FormInterface) {
if (!is_string($child)) {
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface');
if (!is_string($child) && !is_int($child)) {
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
}

if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function add($child, $type = null, array $options = array())
return $this;
}

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

8000 if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/FormBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuild
* If you add a nested group, this group should also be represented in the
* object hierarchy.
*
* @param string|FormBuilderInterface $child
* @param string|FormTypeInterface $type
* @param array $options
* @param string|integer|FormBuilderInterface $child
* @param string|FormTypeInterface $type
* @param array $options
*
* @return FormBuilderInterface The builder object.
*/
Expand Down
16 changes: 7 additions & 9 deletions src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
/**
* Creates an empty form configuration.
*
* @param string $name The form name
* @param string|integer $name The form name
* @param string $dataClass The class of the form's data
* @param EventDispatcherInterface $dispatcher The event dispatcher
* @param array $options The form options
Expand All @@ -154,15 +154,13 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*/
public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, array $options = array())
{
$name = (string) $name;

self::validateName($name);

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

$this->name = $name;
$this->name = (string) $name;
$this->dataClass = $dataClass;
$this->dispatcher = $dispatcher;
$this->options = $options;
Expand Down Expand Up @@ -895,15 +893,15 @@ public function getFormConfig()
/**
* Validates whether the given variable is a valid form name.
*
* @param string $name The tested form name.
* @param string|integer $name The tested form name.
*
* @throws UnexpectedTypeException If the name is not a string.
* @throws UnexpectedTypeException If the name is not a string or an integer.
* @throws \InvalidArgumentException If the name contains invalid characters.
*/
public static function validateName($name)
{
if (!is_string($name)) {
throw new UnexpectedTypeException($name, 'string');
if (null !== $name && !is_string($name) && !is_int($name)) {
throw new UnexpectedTypeException($name, 'string, integer or null');
}

if (!self::isValidName($name)) {
Expand All @@ -930,6 +928,6 @@ public static function validateName($name)
*/
public static function isValidName($name)
{
return '' === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FormFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function create($type = 'form', $data = null, array $options = array(), F
*
* @see createNamedBuilder()
*
* @param string $name The name of the form
* @param string|integer $name The name of the form
* @param string|FormTypeInterface $type The type of the form
* @param mixed $data The initial data
* @param array $options The options
Expand Down Expand Up @@ -83,7 +83,7 @@ public function createBuilder($type = 'form', $data = null, array $options = arr
/**
* Returns a form builder.
*
* @param string $name The name of the form
* @param string|integer $name The name of the form
* @param string|FormTypeInterface $type The type of the form
* @param mixed $data The initial data
* @param array $options The options
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/FormInterface.php
F438
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function getParent();
/**
* Adds a child to the form.
*
* @param FormInterface|string $child The FormInterface instance or the name of the child.
* @param string|null $type The child's type, if a name was passed.
* @param array $options The child's options, if a name was passed.
* @param FormInterface|string|integer $child The FormInterface instance or the name of the child.
* @param string|null $type The child's type, if a name was passed.
* @param array $options The child's options, if a name was passed.
*
* @return FormInterface The form instance
*
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ public function testAddUsingNameAndType()
$this->assertSame(array('foo' => $child), $this->form->all());
}

public function testAddUsingIntegerNameAndType()
{
$child = $this->getBuilder(0)->getForm();

$this->factory->expects($this->once())
->method('createNamed')
->with('0', 'text', null, array('bar' => 'baz'))
->will($this->returnValue($child));

// in order to make casting unnecessary
$this->form->add(0, 'text', array('bar' => 'baz'));

$this->assertTrue($this->form->has(0));
$this->assertSame($this->form, $child->getParent());
$this->assertSame(array(0 => $child), $this->form->all());
}

public function testAddUsingNameButNoType()
{
$this->form = $this->getBuilder('name', null, '\stdClass')
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Form/Tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function testNoSetName()
$this->assertFalse(method_exists($this->builder, 'setName'));
}

public function testAddNameNoString()
public function testAddNameNoStringAndNoInteger()
{
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->builder->add(1234);
$this->builder->add(true);
}

public function testAddTypeNoString()
Expand Down Expand Up @@ -82,6 +82,13 @@ public function testAdd()
$this->assertTrue($this->builder->has('foo'));
}

public function testAddIntegerName()
{
$this->assertFalse($this->builder->has(0));
$this->builder->add(0, 'text');
$this->assertTrue($this->builder->has(0));
}

public function testAll()
{
$this->factory->expects($this->once())
Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/Form/Tests/FormConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Form\Tests;

use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
Expand All @@ -21,8 +23,6 @@ class FormConfigTest extends \PHPUnit_Framework_TestCase
public function getHtml4Ids()
{
return array(
array('a0', true),
array('a9', true),
array('z0', true),
array('A0', true),
array('A9', true),
Expand Down Expand Up @@ -53,6 +53,16 @@ public function getHtml4Ids()
// For root forms, leading underscores will be stripped from the
// "id" attribute to produce valid HTML4.
array('_', true),
// Integers are allowed
array(0, true),
array(123, true),
// NULL is allowed
array(null, true),
// Other types are not
array(1.23, false),
array(5., false),
array(true, false),
array(new \stdClass(), false),
);
}

Expand All @@ -68,6 +78,11 @@ public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
if (!$accepted) {
$this->fail(sprintf('The value "%s" should not be accepted', $name));
}
} catch (UnexpectedTypeException $e) {
// if the value was not accepted, but should be, rethrow exception
if ($accepted) {
throw $e;
}
} catch (\InvalidArgumentException $e) {
// if the value was not accepted, but should be, rethrow exception
if ($accepted) {
Expand Down
0