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

Skip to content

[Form][ButtonBuilder] Prevent buttons names from starting with a capital letter #34445

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 29 additions & 6 deletions src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/Tests/ButtonBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down