I'm trying to create a form with EntityType but is not running.
This is my BD:
I already got Users and AcademiucProgram, so i want to create a register between User and AcameicProgram.
From my controller it provides a variable to my form wich contains the ID of the user.
My controller:
public function indexAction(Request $request,$id){
$ac = new Usersacademi();
$form = $this->createForm(UsersacademiType::class,$ac,array('id'=>$id));
$form->handleRequest($request);
if($form->isValid()){
$ac->setIdacademicprogram($form->get("idacademicprogram")->getData());
$ac->setIduser($form->get("iduser")->getData());
$em = $this->getDoctrine()->getManager();
$em->persist($ac);
$flush = $em->flush();
}
else{
}
return $this->render("AppBundle:admin:apteacher.html.twig", array(
"form" => $form->createView()
));
}
My Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('idacademicprogram', EntityType::class, array(
"required"=>"required",
'class' => 'AppBundle:Academicprogram',
'choice_label' => 'name'
))
->add('iduser', EntityType::class, array("required"=>"required",
"data" =>$options["id"],
'class' => 'AppBundle:User',
"attr"=>array(
"class" => "form-iduser form-control"
)))
->add('Registrar',SubmitType::class, array("attr"=>array(
"class" => "form-submit btn btn-success"
)));
}
So i want to be able to select the AcademicProgram with a Select Option (this is actually running) and then on the second field (idUser) i want that by default is the id of the user selected (that id number is provided by the controller) and then be able to submit this register.
Looks like you forgot to add choice_label
in your user form field or you can add a __toString() method inside your User entity.
From the docs:
If left blank, the entity object will be cast to a string and so must have a
__toString()
method.
By default EntityType already uses entity id
as value.