2

I have a Form with an EntityType field. The table from which the values are taken has grown, and the select box that is rendered makes the page to big (=slow to load).

I replaced this:

        ->add(
            'contact',
            'entity',
            array(
                'class' => 'CRMCoreBundle:Contact',
                'required' => false,
                'empty_data' => null,
            )
        )

with:

          ->add(
            'contact',
            'entity',
            array(
                'class' => 'CRMCoreBundle:Contact',
                'choices' => array(),
                'required' => false,
                'empty_data' => null,
            )
        )

to render an empty selectbox, and on the frontend side I use AJAX to populate and autocomplete the selectbox.

The problem is that now when I submit the form, it is not valid. Any ideas?

1 Answer 1

3

It does not pass validation because the values you are submitting were not added by form component when the form was created. This is to protect the form from accepting unauthorized values.

The proper way to do that is to have your ajax request the form to update the select field on backend using form events, and then update the displayed select with proper values.

More on form events here - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

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

4 Comments

thanks for your reply. I already read that, and none of the use cases described there work for me (I guess the third one is the closest).
also, I suspect they didn't test it recently. in their example they add a field to the form in the POST_SUBMIT event handler. But when you try to do that an exception is thrown: "You cannot add children to a submitted form 500 Internal Server Error - AlreadySubmittedException"
It works when you add POST_SUBMIT event listener to a field, not to the whole form.
Also, take a look here - showmethecode.es/php/symfony/symfony2-4-dependent-forms . Some more info on dynamic forms.

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.