Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Martin Hujer:
Don't Use Entities in Symfony Forms. Use Custom Data Objects Instead
Aug 28, 2017 @ 10:53:14

In this post to his site Martin Hujer suggests that you don't use entities directly in your Symfony forms, opting instead for custom functionality to persist form data.

Let's start with stating that using entities for validation in Symfony Forms is widely used and widely recommend approach. Even the official documentation suggests it.

And I don't think it is a good idea! Why? [Three reasons:] an entity should be always valid, change [in the future and] layers separation.

For each of these downfalls he gets into a bit of detail about what the issue is and introduces the alternative: a custom data class. This class is then used to represent the data in the form with some simple assertions. He includes an example of this kind of class with three properties: title, content and public date. He then shows how to build a form using this class and how to handle updates, not just creates, with the same functionality.

tagged: tutorial entity symfony form data persistence custom class

Link: https://blog.martinhujer.cz/symfony-forms-with-request-objects/