Allow to filter parameters by their (numeric) value

This commit is contained in:
Jan Böhmer 2022-09-08 00:04:53 +02:00
parent b56a970d5b
commit dd400ae70c
8 changed files with 288 additions and 23 deletions

View file

@ -13,6 +13,17 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class NumberConstraintType extends AbstractType
{
protected const CHOICES = [
'' => '',
'=' => '=',
'!=' => '!=',
'<' => '<',
'>' => '>',
'<=' => '<=',
'>=' => '>=',
'filter.number_constraint.value.operator.BETWEEN' => 'BETWEEN',
];
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
@ -31,17 +42,6 @@ class NumberConstraintType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$choices = [
'' => '',
'=' => '=',
'!=' => '!=',
'<' => '<',
'>' => '>',
'<=' => '<=',
'>=' => '>=',
'filter.number_constraint.value.operator.BETWEEN' => 'BETWEEN',
];
$builder->add('value1', NumberType::class, array_merge_recursive([
'label' => 'filter.number_constraint.value1',
'attr' => [
@ -68,7 +68,7 @@ class NumberConstraintType extends AbstractType
$builder->add('operator', ChoiceType::class, [
'label' => 'filter.number_constraint.operator',
'choices' => $choices,
'choices' => static::CHOICES,
'required' => false,
]);
}

View file

@ -41,6 +41,9 @@ class ParameterConstraintType extends AbstractType
//'required' => false,
] );
$builder->add('value', ParameterValueConstraintType::class, [
]);
/*
* I am not quite sure why this is needed, but somehow symfony tries to create a new instance of TextConstraint
* instead of using the existing one for the prototype (or the one from empty data). This fails as the constructor of TextConstraint requires

View file

@ -0,0 +1,33 @@
<?php
namespace App\Form\Filters\Constraints;
class ParameterValueConstraintType extends NumberConstraintType
{
protected const CHOICES = [
'' => '',
'filter.parameter_value_constraint.operator.=' => '=',
'filter.parameter_value_constraint.operator.!=' => '!=',
'filter.parameter_value_constraint.operator.<' => '<',
'filter.parameter_value_constraint.operator.>' => '>',
'filter.parameter_value_constraint.operator.<=' => '<=',
'filter.parameter_value_constraint.operator.>=' => '>=',
'filter.parameter_value_constraint.operator.BETWEEN' => 'BETWEEN',
//Extensions by ParameterValueConstraint
'filter.parameter_value_constraint.operator.IN_RANGE' => 'IN_RANGE',
'filter.parameter_value_constraint.operator.NOT_IN_RANGE' => 'NOT_IN_RANGE',
'filter.parameter_value_constraint.operator.GREATER_THAN_RANGE' => 'GREATER_THAN_RANGE',
'filter.parameter_value_constraint.operator.GREATER_EQUAL_RANGE' => 'GREATER_EQUAL_RANGE',
'filter.parameter_value_constraint.operator.LESS_THAN_RANGE' => 'LESS_THAN_RANGE',
'filter.parameter_value_constraint.operator.LESS_EQUAL_RANGE' => 'LESS_EQUAL_RANGE',
'filter.parameter_value_constraint.operator.RANGE_IN_RANGE' => 'RANGE_IN_RANGE',
'filter.parameter_value_constraint.operator.RANGE_INTERSECT_RANGE' => 'RANGE_INTERSECT_RANGE'
];
public function getParent(): string
{
return NumberConstraintType::class;
}
}