Reputation: 1522
I have two entities Student and Classes and I want to create a form shows the student details and a dropdown / chocelist of classes and assign one class to the student. Also the classes should come from the DB table so the controller is like this.
public function studentAddClassAction( $id, Request $request )
{
// get the student from the student table
$em = $this->getDoctrine()->getManager();
$student= $em->getRepository('PIE10Bundle:Student')->find($id);
// new class object and create the form
$class = new Classes;
$form = $this->createForm(ClassesType::class, $class);
$form->handleRequest($request);
if( $form->isSubmitted() && $form->isValid() )
{
// form operation - update student row with the classID
}
return $this->render(
'PIE10Bundle:student:layout_student_addclass.html.twig',
array(
'student'=> $student,
'title' => 'Add Class',
'tables' => 1,
'form' => $form->createView()
)
);
}
and my ClassesType is like below
class ClassesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('classes',
ClassesType::class,
array(
'class' => 'PIE10Bundle:Classes',
'property' => 'className',
'expanded' => false,
'multiple' => false
));
$builder->add('Add Class',
SubmitType::class,
array('attr' => array('class' => 'btn btn-primary',
'style' => 'margin:15px 0;')) );
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PIE10Bundle\Entity\Classes',
));
}
}
and when I try to access this form I am getting the following 500 Internal Server Error - UndefinedOptionsException error
The options "class", "expanded", "multiple", "property" do not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "description", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "validation_groups".
So I need to know what went wrong and how to fix this. Please let me know if any other information needed. Thanks
Upvotes: 0
Views: 1809
Reputation: 1522
With use of @Yoshi answer I changed the
$builder->add('classes', ClassesType::class, ...
to
$builder->add('classes', EntityType::class, ...
and also added the
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
to the ClassesType file and then updated the Controller like below
public function studentAddClassAction( $id, Request $request )
{
// get the student from the student table
$em = $this->getDoctrine()->getManager();
$student= $em->getRepository('PIE10Bundle:Student')->find($id);
// new class object and create the form
$class = $em->getRepository('PIE10Bundle:Classes')->findAll();
$form = $this->createForm(ClassesType::class, $class);
$form->handleRequest($request);
if( $form->isSubmitted() && $form->isValid() )
{
// form operation - update student row with the classID
}
return $this->render(
'PIE10Bundle:student:layout_student_addclass.html.twig',
array(
'student'=> $student,
'title' => 'Add Class',
'tables' => 1,
'form' => $form->createView()
)
);
}
and then the ClassesType as follow
class ClassesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('classes',
EntityType::class,
array(
'class' => 'PIE10Bundle:Classes',
'expanded' => false,
'multiple' => false
));
$builder->add('Add Class',
SubmitType::class,
array('attr' => array('class' => 'btn btn-primary',
'style' => 'margin:15px 0;')) );
}
}
@Yoshi answer helped me to fix the error
Upvotes: 0
Reputation: 54649
In ClassesType
change:
$builder->add('classes', ClassesType::class, ...
to:
$builder->add('classes', EntityType::class, ...
also add:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
at the top of your file, for it to be available.
Currently you're using ClassesType
itself (which would problably result in an infinite loop if it weren't for the errors). This type does not extend the EntityType
and thus the options you're using (class
, expanded
, multiple
, property
) do not exist.
Also not that property
is deprecated, you should use choice_label/choice_value
unless you're using a rather old version of symfony.
Reference: EntityType Field
Upvotes: 1