From a865f1764728936a7c15d84caafaee38ceffed39 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Dec 2024 20:58:05 +0100 Subject: [PATCH] Add validation for component properties --- composer.json | 1 + src/Twig/Component/Banner.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/composer.json b/composer.json index bcb58517ed..880303d05a 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/http-foundation": "^5.4|^6.0|^7.0", "symfony/http-kernel": "^5.4|^6.0|^7.0", "symfony/intl": "^5.4|^6.0|^7.0", + "symfony/options-resolver": "^5.4|^6.0|^7.0", "symfony/property-access": "^5.4|^6.0|^7.0", "symfony/security-bundle": "^5.4|^6.0|^7.0", "symfony/string": "^5.4|^6.0|^7.0", diff --git a/src/Twig/Component/Banner.php b/src/Twig/Component/Banner.php index 63de97d820..9d2a1fc981 100644 --- a/src/Twig/Component/Banner.php +++ b/src/Twig/Component/Banner.php @@ -3,12 +3,30 @@ namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component; use EasyCorp\Bundle\EasyAdminBundle\Config\Enum\BannerVariant; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\UX\TwigComponent\Attribute\PreMount; class Banner { public BannerVariant|string $variant = BannerVariant::Info; public bool $withDismissButton = false; + #[PreMount] + public function preMount(array $data): array + { + $resolver = new OptionsResolver(); + $resolver->setIgnoreUndefined(true); + + $resolver->define('variant') + ->allowedTypes('string', BannerVariant::class) + ->allowedValues(...array_map(static fn(BannerVariant $variant): string => $variant->value, BannerVariant::cases())); + + $resolver->define('withDismissButton') + ->allowedTypes('bool'); + + return $resolver->resolve($data) + $data; + } + public function getVariantCssClass(): string { return \is_string($this->variant) ? $this->variant : $this->variant->asCssClass();