Create FormFactory or not?

materix
Generous Backer | 46
+
0
-

I apologize for this noob question.

I often see that people use FormFactory for creating forms.

For example:

class FormFactory
{

	use Nette\SmartObject;

	public function create(): Form
	{
		return new Form();
	}

}

What is the advantage of this approach, in comparison with just calling new Form() directly?

stepos2
Member | 33
+
+2
-

There is no advantage if you just use this factory.

It helps with adding stuff that you need in all forms, so you don't have to repeat yourself. For example:

class FormFactory
{
	public function create(): Form
	{
		$form = new Form;
		$form->addProtection();
		return $form;
	}
}
dkorpar
Member | 127
+
+1
-

it's also about where to put code, if U put everything in presenters code will get messy and hard to read so it's better to have forms (almost any component) defined on their own. As a bonus you can also then easily reuse that form on multiple pages if needed.