Search code examples
phpsymfonysymfony4

GET max value form since DB symfony 4


Sorry to disturb you but I would like to obtain a max value for a form validation for example it can be a numeric field but the validation of type range MaxValue will have to be retrieved in the database I explain myself for example I wish to reserve a transport the max value by default is 4 for 4 remaining places but if for this vehicle there are 3 places left, the validation would have to pass to 3

I tried to go through entitytype with events but it didn't change anything to my problem it returned in the drop down list all the number of places for each vehicle.

The goal is to make a simple form a reservation button with just a way to choose the number of places while not being able to exceed the value of the database.

For example the URL is /transport/6/book

We must find a way to pass the value of id number 6 and the validation would be between 1 and 3 for example or create a list that puts 1 2 3

If transport 7 to 1 place of availability it would have to be 1 only.

Excuse me if I misspoke but I've been working on it for more than a week and I can't get it to work so I've moved to an old commit to keep the application working. I'm a beginner in this area so I don't know the advanced features of the form but I'm very good at the twig repository etc. It's just this part that is blocking, thanks in advance for your help.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nbPlaces', EntityType::class, [
                'class' => Transport::class,
                'label' => 'Nombre de places',
                'attr' => [
                    'placeholder' => "Nombre de places",
                ],
                'query_builder' => function (TransportRepository $transportRepository) use ($id_service) {
                    return $transportRepository->findOneByIdPlace($id_service);
                },
                'choice_label' => 'nbPlaces'
            ]);
    }

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Reservation::class,
        'id_service' => null
    ]);
}

and controller

$booking = new Reservation();
        $form = $this->createForm(ReservationType::class, $booking, ['id_service' => $service]);

and $id_service is undefined variable in use


Solution

  • It's because $id_service is never defined. You should get it from the options. Take a look here : https://symfony.com/doc/current/forms.html#other-common-form-features

    So your code should be a thing like this.

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $id_service = $options['id_service'];
        $builder
            ->add('nbPlaces', EntityType::class, [
                'class' => Transport::class,
                'label' => 'Nombre de places',
                'attr' => [
                    'placeholder' => "Nombre de places",
                ],
                'query_builder' => function (TransportRepository $transportRepository) use ($id_service) {
                    return $transportRepository->findOneByIdPlace($id_service);
                },
                'choice_label' => 'nbPlaces'
            ]);
    }