Symfony2: get rid of “This form should not contain

2019-07-04 02:11发布

问题:

I have added a second submit button to my form, now Symfony2 complains about it: "This form should not contain extra fields"

Although I added this option in the formtype:

public function getDefaultOptions(array $options)
    {
        return array(
            'csrf_protection' => false,
        );
    }

Any ideas?

回答1:

You can most certainly have multiple submit buttons. Make sure the button is not in the same array as the other form fields.

So, for example, if your form fields have a name FormType[field_name], you can't have FormType[submit_btn] as the name of the button and you must choose a different one.

Your controller can act differently depending on the button pressed. If your submit buttons are named submit_1 and submit_2 you can have something similar to

if($this->getRequest()->request->has('submit_1')) {
    // do stuff
} else {
    // do other stuff
}


标签: forms symfony