From 3e87ecf08e6fe3b8412f1ead990e5a2a162657b8 Mon Sep 17 00:00:00 2001 From: Maciej Date: Thu, 27 Jul 2023 08:24:44 +0200 Subject: [PATCH] OP-180 - adds validation for price filters --- src/Form/Type/PriceFilterType.php | 8 ++++++++ src/Resources/translations/validators.en.yml | 1 + 2 files changed, 9 insertions(+) diff --git a/src/Form/Type/PriceFilterType.php b/src/Form/Type/PriceFilterType.php index f8cc0c4a..f43ace61 100644 --- a/src/Form/Type/PriceFilterType.php +++ b/src/Form/Type/PriceFilterType.php @@ -18,11 +18,13 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Validator\Constraints\LessThan; use Symfony\Component\Validator\Constraints\PositiveOrZero; use Symfony\Component\Validator\Constraints\Type; final class PriceFilterType extends AbstractFilterType { + public const MAXIMUM_PRICE_VALUE = 9999999999999999; private PriceNameResolverInterface $priceNameResolver; private CurrencyContextInterface $currencyContext; @@ -50,6 +52,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void new PositiveOrZero([ 'message' => 'bitbag_sylius_elasticsearch_plugin.min_price_positive_or_zero', ]), + new LessThan(PriceFilterType::MAXIMUM_PRICE_VALUE, options: [ + 'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large' + ]) ], ]) ->add($this->priceNameResolver->resolveMaxPriceName(), MoneyType::class, [ @@ -64,6 +69,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void new PositiveOrZero([ 'message' => 'bitbag_sylius_elasticsearch_plugin.max_price_positive_or_zero', ]), + new LessThan(PriceFilterType::MAXIMUM_PRICE_VALUE, options: [ + 'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large' + ]) ], ]) ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { diff --git a/src/Resources/translations/validators.en.yml b/src/Resources/translations/validators.en.yml index 8de6bb1b..ea32f868 100644 --- a/src/Resources/translations/validators.en.yml +++ b/src/Resources/translations/validators.en.yml @@ -3,3 +3,4 @@ bitbag_sylius_elasticsearch_plugin: max_price_numeric: Max price must be a valid price min_price_positive_or_zero: Min price cannot be negative max_price_positive_or_zero: Max price cannot be negative + price_value_too_large: Provided price value is too large