Skip to content

Commit

Permalink
Add validation for component properties
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Dec 12, 2024
1 parent f0c9bd2 commit a865f17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions src/Twig/Component/Banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a865f17

Please sign in to comment.