Skip to content

Commit

Permalink
OP-180 - adds validation for price filters
Browse files Browse the repository at this point in the history
  • Loading branch information
MacBalc committed Jul 27, 2023
1 parent a1f72b5 commit 3e87ecf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Form/Type/PriceFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, [
Expand All @@ -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) {
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 3e87ecf

Please sign in to comment.