Skip to content

Commit e6acf8a

Browse files
committed
Added validation for non-empty 'name' option
1 parent 98625f3 commit e6acf8a

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/lib/Twig/Components/AbstractChoiceInput.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public function validate(array $props): array
4444
$resolver
4545
->define('name')
4646
->required()
47-
->allowedTypes('string');
47+
->allowedTypes('string')
48+
->allowedValues(static function (string $value): bool {
49+
return trim($value) !== '';
50+
});
4851
$resolver
4952
->define('checked')
5053
->allowedTypes('bool')

tests/integration/Twig/Components/Checkbox/FieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ public function testInvalidIndeterminateTypeCausesResolverErrorOnMount(): void
101101
]));
102102
}
103103

104+
public function testEmptyNameCausesResolverErrorOnMount(): void
105+
{
106+
$this->expectException(InvalidOptionsException::class);
107+
108+
$this->mountTwigComponent(Field::class, $this->baseProps([
109+
'name' => '',
110+
]));
111+
}
112+
104113
public function testEmptyIdCausesResolverErrorOnMount(): void
105114
{
106115
$this->expectException(InvalidOptionsException::class);

tests/integration/Twig/Components/Checkbox/InputTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ public function testInvalidIndeterminateTypeCausesResolverErrorOnMount(): void
104104
]);
105105
}
106106

107+
public function testEmptyNameCausesResolverErrorOnMount(): void
108+
{
109+
$this->expectException(InvalidOptionsException::class);
110+
111+
$this->mountTwigComponent(Input::class, [
112+
'id' => 'agree',
113+
'name' => '',
114+
'value' => 'yes',
115+
]);
116+
}
117+
107118
public function testMissingRequiredOptionsCauseResolverErrorOnMount(): void
108119
{
109120
$this->expectException(MissingOptionsException::class);

tests/integration/Twig/Components/RadioButton/FieldTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ public function testInputBooleanAttributesAndClassesPassThrough(): void
8181
self::assertNotNull($input->attr('required'), 'Required should render native "required" attribute.');
8282
}
8383

84+
public function testEmptyNameCausesResolverErrorOnMount(): void
85+
{
86+
$this->expectException(InvalidOptionsException::class);
87+
88+
$this->mountTwigComponent(
89+
Field::class,
90+
$this->baseProps([
91+
'name' => '',
92+
])
93+
);
94+
}
95+
8496
public function testEmptyIdCausesResolverErrorOnMount(): void
8597
{
8698
$this->expectException(InvalidOptionsException::class);
@@ -92,6 +104,7 @@ public function testEmptyIdCausesResolverErrorOnMount(): void
92104
])
93105
);
94106
}
107+
95108
public function testInvalidAttributesTypeCausesResolverErrorOnMount(): void
96109
{
97110
$this->expectException(InvalidOptionsException::class);

tests/integration/Twig/Components/RadioButton/InputTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public function testSizeVariantSmallAddsClass(): void
112112
self::assertStringContainsString('ids-input--small', $this->getClassAttr($input), 'Size "small" should add "ids-input--small" class.');
113113
}
114114

115+
public function testEmptyNameCausesResolverErrorOnMount(): void
116+
{
117+
$this->expectException(InvalidOptionsException::class);
118+
119+
$this->mountTwigComponent(Input::class, [
120+
'name' => '',
121+
'value' => 'A',
122+
]);
123+
}
124+
115125
public function testInvalidDisabledTypeCausesResolverErrorOnMount(): void
116126
{
117127
$this->expectException(InvalidOptionsException::class);

0 commit comments

Comments
 (0)