I have a form that should allow to add an item (device) to a category (brand). Below is a part of the controller that creates the form (the $brand thing doesn't work but I'll figure that out later). Below that is the code that creates my form.
I want my Select box (which is an entitytype of Brand, and shows all possible brands) to also show a default selected value, based on a variable passed down by the controller.
Two questions:
- where can I pass this value down?
- how can I set a default option for the EntityType select box? I expected it to be 'data' but even hard-coding a number there won't work.
This is the controller bit:
public function createDevice(Request $request, $brand) {
$device = new Device();
$form = $this->createForm(DeviceType::class, $device); // where do I pass the value of the default option?
$form->handleRequest($request);
and the type:
class DeviceType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('brand', EntityType::class, array(
'class' => 'AppBundle:Brand',
'choice_label' => 'name',
'choice_value' => 'id',
'data' => '2' // does not set default value to second item!
$brandas an object via TWIG; but you could via the controller. Just trying to help you. If it was a brand ID, then you could use that in a querybuilder.createDevice(Request $request, $brand), and the variable$brand. Are you attempting to pass a brand object in to the function? If so, it needs to be an object.