1

I have an entity with age.
I created a search form to show records where ageMin < age < ageMax.
So I added non mapped form fields ageMin and ageMax:

$builder
    ->add('ageMin', IntegerType::class, [
        'mapped' => false,
        'required' => false
    ])
    ->add('ageMax', IntegerType::class, [
        'mapped' => false,
        'required' => false
    ])
;

I would like to check if the user has entered min greater than max (very simple requirement!).

I tried Symfony validate form with mapped false form fields but it is for symfony 2 and Components required is confusing for me.

I found these help pages too: https://symfony.com/doc/current/form/without_class.html#form-option-constraints and tried:

->add('ageMin', IntegerType::class, [
            'mapped' => false, 
            'required' => false,
            'constraints' => new LessThan('ageMax')
        ])

There are no errors but it doesn't work.

Other page: https://symfony.com/doc/current/validation/raw_values.html

cezar
  • 11,616
  • 6
  • 48
  • 84
bcag2
  • 1,988
  • 1
  • 17
  • 31

1 Answers1

0

If there is an entity with the property age you could use LessThan for ageMin and GreaterThan for ageMax to compare these values to age.

$builder
    ->add('ageMin', IntegerType::class, [
        'mapped' => false,
        'required' => false,
        'constraints' => [new LessThan(['propertyPath' => 'age'])]
    ])
    ->add('ageMax', IntegerType::class, [
        'mapped' => false,
        'required' => false,
        'constraints' => [new GreaterThan(['propertyPath' => 'age'])]
    ])
;

Take care for proper import of the constraints.

EDIT:

The example above assumes that the form makes use of the entity having the property age. If the name of the entity class is Foo, then the method configureOptions would look like:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Foo::class,
    ]);
}
cezar
  • 11,616
  • 6
  • 48
  • 84
  • yes I have an entity with `age` but my need is just to check that the user doesn't entered an `ageMin` greater than `ageMax` – bcag2 May 21 '19 at 07:25
  • If `ageMin` is less than `age` and `ageMax` is greater than `age`, then `ageMax` is greater than `ageMin`. Both properties are optional, so the users might enter just one of them, and in that case you would be comparing an integer to `null`. You would have to implement additional logic to catch this edge case. If you follow the approach from my answer, you'll have cleaner code that is easier to read and maintain. – cezar May 21 '19 at 08:36
  • This also accomplishes exactly the requirement in your question: *"I created a search form to show records where ageMin < age < ageMax."* – cezar May 21 '19 at 08:38
  • 'constraints' => [new LessThan(['propertyPath' => 'age'])] (based on https://github.com/symfony/symfony/issues/29719) returns error: Invalid property path "age" provided to "Symfony\Component\Validator\Constraints\LessThan" constraint: Neither the property "age" nor one of the methods "getAge()", "age()", "isAge()", "hasAge()", "__get()" exist and have public access in class "Symfony\Component\Form\Form". – bcag2 May 21 '19 at 10:44
  • @bcag2 Please check the edit. It is probably because you haven't set up the `configureOptions` in your form class. – cezar May 21 '19 at 12:10
  • 1
    Constraints configured with the `constraints` option of the form are attached to the built form. I suggest that you add the constraints to the underlying class instead. In this case the property should work as expected. – xabbuh Jun 03 '19 at 08:30
  • @xabbuh Thank you very much for your comment. Could you please provide a code example how the `constraints` should be properly added? – cezar Jun 03 '19 at 09:05
  • I am not sure that's actually possible. – xabbuh Jun 03 '19 at 09:46