Symfony2 invalid form still saves entity on flush

让人想犯罪 __ 提交于 2020-03-18 09:56:14

问题


I currently have a form and on save I always make an API call to validate the form information against an external source (eg: is the company a VAT paying company etc.). I also want to store in the database the response I get from the external API, for which I have a post submit subscriber on the form.

In the subscriber, I call the API and set errors on the form where values do not match. That is where I call a service to handle all the business logic and somewhere in there I have

$em->persist($apiResponse);
$em->flush();

to hold onto the API response.

This ends up saving the entity values attached to the form (company) in the database even though these values are actually invalid and the user is returned to the form page saying that.

Please note this happens on edit, and I understand why it happens, because the entity is managed by Doctrine.

I also tried flushing only the entity I am interested in:

$em->persist($apiResponse);
$em->flush($apiResponse);

and this seems to work, but I read this can have an unexpected behavior.

Hence, my question is: what are the solutions for this ?

Thank you, and sorry for the lack of code samples!

来源:https://stackoverflow.com/questions/33656366/symfony2-invalid-form-still-saves-entity-on-flush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!