Skip to content

Commit 4df6dfa

Browse files
committed
Enhanced ChoiceInputLabel validation to disallow empty 'for' values
1 parent ff0956e commit 4df6dfa

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/lib/Twig/Components/ChoiceInputLabel.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public function validate(array $props): array
3636
$resolver
3737
->define('for')
3838
->required()
39-
->allowedTypes('string');
39+
->allowedTypes('string')
40+
->allowedValues(static function (string $value): bool {
41+
return trim($value) !== '';
42+
});
4043

4144
return $resolver->resolve($props) + $props;
4245
}

tests/integration/Twig/Components/ChoiceInputLabelTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function testAttributesMergeClassAndData(): void
7171
self::assertSame('custom', $label->attr('data-custom'), 'Custom data attribute should be rendered on the label.');
7272
}
7373

74+
public function testEmptyForCausesResolverErrorOnMount(): void
75+
{
76+
$this->expectException(InvalidOptionsException::class);
77+
78+
$this->mountTwigComponent(ChoiceInputLabel::class, ['for' => '', 'content' => 'x']);
79+
}
80+
7481
public function testInvalidForTypeCausesResolverErrorOnMount(): void
7582
{
7683
$this->expectException(InvalidOptionsException::class);

0 commit comments

Comments
 (0)