Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe82230

Browse files
committed
[Form][ButtonBuilder] Prevent buttons names from starting with a capital letter
1 parent 15f7ea1 commit fe82230

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/Symfony/Component/Form/ButtonBuilder.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,14 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
5050
private $options;
5151

5252
/**
53-
* @throws InvalidArgumentException if the name is empty
53+
* @throws InvalidArgumentException if the name is empty or invalid
5454
*/
5555
public function __construct(?string $name, array $options = [])
5656
{
57-
if ('' === $name || null === $name) {
58-
throw new InvalidArgumentException('Buttons cannot have empty names.');
59-
}
57+
self::validateName($name);
6058

6159
$this->name = $name;
6260
$this->options = $options;
63-
64-
FormConfigBuilder::validateName($name);
6561
}
6662

6763
/**
@@ -757,4 +753,31 @@ public function getIterator()
757753
{
758754
return new \EmptyIterator();
759755
}
756+
757+
/**
758+
* Validates whether the given variable is a valid button name.
759+
*
760+
* @throws InvalidArgumentException if the name is empty or contains invalid characters
761+
*
762+
* @internal
763+
*/
764+
final public static function validateName(?string $name)
765+
{
766+
if ('' === $name || null === $name) {
767+
throw new InvalidArgumentException('Buttons cannot have empty names.');
768+
}
769+
770+
if (!self::isValidName($name)) {
771+
throw new InvalidArgumentException(sprintf('The name "%s" contains illegal characters. Names should start with a lowercase letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").', $name));
772+
}
773+
}
774+
775+
/**
776+
* A name is accepted if it starts with a lowercase letter, digit or underscore
777+
* and contains only letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").
778+
*/
779+
final public static function isValidName(string $name): bool
780+
{
781+
return preg_match('/^[a-z0-9_][a-zA-Z0-9_\-:]*$/', $name);
782+
}
760783
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ public function testValidNames($name)
4242
public function testNameContainingIllegalCharacters()
4343
{
4444
$this->expectException('Symfony\Component\Form\Exception\InvalidArgumentException');
45-
$this->expectExceptionMessage('The name "button[]" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").');
45+
$this->expectExceptionMessage('The name "button[]" contains illegal characters. Names should start with a lowercase letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").');
4646

4747
$this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder('button[]'));
4848
}
4949

50-
/**
51-
* @group legacy
52-
*/
5350
public function testNameStartingWithIllegalCharacters()
5451
{
52+
$this->expectException(InvalidArgumentException::class);
53+
$this->expectExceptionMessage('The name "Button" contains illegal characters. Names should start with a lowercase letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").');
54+
5555
$this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder('Button'));
5656
}
5757

0 commit comments

Comments
 (0)