3
votes

I need to add additional form fields dynamically with different names and ids to the form using angularJS. How do I make symfony ignore those fields when checking if the form is valid? I cant add those fields to the builder because I don't know what the names of the field will be. I could use form field naming including ID for example field1, field2, field3 and so on. but if so can I set a pattern of the field somehow? something like field*

2

2 Answers

1
votes

Despite that fact that I agree with @sepikas_antanas, if you desperately want to go that path you can add unknown form fields using submitted data:

class ExampleEntry extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $f = function(FormEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();
            if (is_array($data)) {
                foreach ($data as $name => $value) {
                    if (!$form->has($name)) {
                        // dunno you fields types 
                        $form->add($name, 'hidden');
                    }
                }
            }
        };
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $f);
        $builder->addEventListener(FormEvents::PRE_SUBMIT, $f);             
    }
0
votes

According to symfony2 documentation http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

You should organize your app in such way that user actions (those who add form fields) would submit form with data needed to generate form field:

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

Symfony2 form generation only happens on server side, that is for security reasons.

Ignoring form validation is not a good practice and in my opinion should be avoided altogether.