I have a Symfony Form Type and I want to inject on it another form I have Created:
$form = $this->createFormBuilder();
$form->add('quantity', NumberType::class);
$form->add('options', ProductOptionType::class);
$form->add('notes', TextareaType::class, ]);
ProductOptionType is another type I have created but on this type I inject on constructor the current product so I know then what available options this product has in order to add in the form (ie color, size etc).
class ProductOptionType extends AbstractType
{
private $productOptions;
public function __construct(Product $product)
{
$this->productOptions = $product->getOptions();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->productOptions as $option) {
$builder->add($option->getId(), ChoiceType::class, [
'choices' => $option->getChoicesForForm(),
'label' => $option->getName()
]);
}
}
}
The Problem is how to pass the constructor here???
$form->add('options', ProductOptionType::class);
The above throws contstructor none error as it instantiates the type without passign to the injector the product.
Also this one throws exception:
$form->add('options', new ProductOptionType($product));
Because it expects a string and not an object.