I have a form that I want to edit based on the errors occurred during its validation but when I try to delete a field I get the exception "AlreadySubmittedException".
How to delete a field from my form after the submit?
$form = $this->createForm(MyForm::class);
$removeTheField=true;
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
// Send form data to an API and get api errors
// if api responds with some error
$removeTheField = false;
// endif
}
}
if(removeTheField)) {
$form->remove('my_field'); // throw AlreadySubmittedException
}
One solution that I am considering but which I do not like too much would be to recreate the form again with the data of the previous one and the field in less.