Skip to content

Commit

Permalink
Merge pull request #228 from BitBagCommerce/OP-180-validation-for-fil…
Browse files Browse the repository at this point in the history
…ter-values

OP-180 - adds validation for price filters
  • Loading branch information
senghe authored Sep 13, 2023
2 parents 5573cc0 + fee195c commit c24805e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Form/Type/PriceFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
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;
Expand Down Expand Up @@ -50,6 +53,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
new PositiveOrZero([
'message' => 'bitbag_sylius_elasticsearch_plugin.min_price_positive_or_zero',
]),
new LessThan(self::MAXIMUM_PRICE_VALUE, options: [
'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large',
]),
],
])
->add($this->priceNameResolver->resolveMaxPriceName(), MoneyType::class, [
Expand All @@ -64,6 +70,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
new PositiveOrZero([
'message' => 'bitbag_sylius_elasticsearch_plugin.max_price_positive_or_zero',
]),
new LessThan(self::MAXIMUM_PRICE_VALUE, options: [
'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large',
]),
],
])
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c24805e

Please sign in to comment.