From 0eab1fd85bede5a3ffc91f9f262173d50c3b16a6 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Mon, 18 Nov 2019 22:17:39 +0100 Subject: [PATCH] [Form][ButtonBuilder] Prevent buttons names from starting with a capital letter --- src/Symfony/Component/Form/ButtonBuilder.php | 35 +++++++++++++++---- .../Form/Tests/ButtonBuilderTest.php | 8 ++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index 5a85f8bc73cfe..d111176626c0d 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -50,18 +50,14 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface private $options; /** - * @throws InvalidArgumentException if the name is empty + * @throws InvalidArgumentException if the name is empty or invalid */ public function __construct(?string $name, array $options = []) { - if ('' === $name || null === $name) { - throw new InvalidArgumentException('Buttons cannot have empty names.'); - } + self::validateName($name); $this->name = $name; $this->options = $options; - - FormConfigBuilder::validateName($name); } /** @@ -757,4 +753,31 @@ public function getIterator() { return new \EmptyIterator(); } + + /** + * Validates whether the given variable is a valid button name. + * + * @throws InvalidArgumentException if the name is empty or contains invalid characters + * + * @internal + */ + final public static function validateName(?string $name): void + { + if ('' === $name || null === $name) { + throw new InvalidArgumentException('Buttons cannot have empty names.'); + } + + if (!self::isValidName($name)) { + 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)); + } + } + + /** + * A name is accepted if it starts with a lowercase letter, digit or underscore + * and contains only letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":"). + */ + final public static function isValidName(string $name): bool + { + return (bool) preg_match('/^[a-z0-9_][a-zA-Z0-9_\-:]*$/D', $name); + } } diff --git a/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php b/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php index 2846d1cc3de03..efd03354c5aa3 100644 --- a/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php +++ b/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php @@ -42,16 +42,16 @@ public function testValidNames($name) public function testNameContainingIllegalCharacters() { $this->expectException('Symfony\Component\Form\Exception\InvalidArgumentException'); - $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 (":").'); + $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 (":").'); $this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder('button[]')); } - /** - * @group legacy - */ public function testNameStartingWithIllegalCharacters() { + $this->expectException(InvalidArgumentException::class); + $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 (":").'); + $this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder('Button')); }