4

I have the following form that contains data from the database it still WIP ( i'm missing a few fields that i didn't add yet). The form loads data in the first select and based on that select i use ajax to populate a second select with options based on the first select ( basically the associations to the selected value). And from there again another select with certain options and so on and at the end when i submit the form i want to generate a report from database based on the data. For the moment i'm stuck with the second field because i always get an error:

This value is not valid.

The form class:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('survey', EntityType::class, [
            'class' => SurveyManager::class,
            'placeholder' => 'Choose option',
            'attr' => [
                'class' => 'field-change',
            ],
        ])
        ->add('headquarter', ChoiceType::class, [
            'choices' => [],
        ])
        ->add('submit', SubmitType::class, [
            'label' => 'Save',
        ])
    ;
}

I'm not reall sure how to fix the error or how should i handle this type of form. Can you help me out guys ?

Based on the answer i did this

$builder->addEventListener(
    FormEvents::PRE_SUBMIT,
    function (FormEvent $event) {
        $form = $event->getForm();

        $data = $event->getData();

        $form->add('headquarter', EntityType::class, [
            'class' => HeadQuarterManager::class,
            'query_builder' => function(HeadQuarterManagerRepository $er) {

                return $er->getHeadquarter($data['survey']);
            },
        ]);
    }
);

But i'm getting this error:

Notice: Undefined variable: data 

Not really sure how to pass the data to the getHeadquarter method so i can return an array of id => name for the select.

2 Answers 2

4

When you run the function $form->isValid(), it checks against the form it built in the buildForm function. Any extra fields/value that aren't there will cause this error.

You can change this behaviour by using form events.

Sign up to request clarification or add additional context in comments.

3 Comments

i was thinking your going to say that... :-D the problem is i've never used them and i didn't quite understand the documentation :(
@L.S from the sounds of it you need option 3 symfony.com/doc/current/form/… When you've done it once you get used to it
ok let me check out that portion put some code togther and we'll see from there what stupid things i did . :D
2

In the end this is how i did it:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('survey', EntityType::class, [
            'class' => SurveyManager::class,
            'attr' => [
                'class' => 'field-change',
            ],
        ])
        ->add('submit', SubmitType::class, [

        ])
        ->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $form = $event->getForm();

                $data = $event->getData();
                $modifier = $data['survey'];
                $form->add('headquarter', EntityType::class, [
                    'class' => HeadQuarterManager::class,
                    'query_builder' => function (HeadQuarterManagerRepository $er) use ($modifier) {
                        return $er->getHeadquarter($modifier);
                    },
                ]);
            }
        );
}

1 Comment

As you have done, is a correct way to do it in symfony (ref: symfony.com/doc/current/form/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.