How to Configure empty Data for a Form Class

The empty_data option allows you to specify an empty data set for your
form class. This empty data set would be used if you submit your form, but
haven't called setData() on your form or passed in data when you created
your form. For example:

1
2
3
4
5
6
7
8
9
10
11
12

publicfunctionindexAction(){$blog=...;// $blog is passed in as the data, so the empty_data// option is not needed$form=$this->createForm(BlogType::class,$blog);// no data is passed in, so empty_data is// used to get the "starting data"$form=$this->createForm(BlogType::class);}

By default, empty_data is set to null. Or, if you have specified
a data_class option for your form class, it will default to a new instance
of that class. That instance will be created by calling the constructor
with no arguments.

If you want to override this default behavior, there are two ways to do this.

You can instantiate your class however you want. In this example, you pass
some dependency into the BlogType then use that to instantiate the Blog class.
The point is, you can set empty_data to the exact "new" object that you want to use.