0

I have StudentCourse entity with this column

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User\RegisteredUser", inversedBy="studentCourses")
 * @ORM\JoinColumn(nullable=false)
 */
private $student;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Course\Course", inversedBy="studentCourses")
 * @ORM\JoinColumn(nullable=false)
 */
private $course;

Then I have form type

namespace App\Form\StudentCourse;

use App\DataHolder\StudentCourse\StudentCourseNewDetails;
use App\Entity\Course\Course;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NewStudentCourseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder
            ->add('course', EntityType::class, [
                'class' => Course::class,
                'choice_label' => 'title'
            ])
            ->add('student', HiddenType::class, [
                'data'=>$options['student']
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => StudentCourseNewDetails::class
        ]);
    }
}

and in controller

$form = $this->createForm(NewStudentCourseType::class, $scnd, [
                'student' => $user
        ]);

when i try display form this error i obtain

The option "student" does not exist. Defined options are: "action", "allow_extra_fields", "allow_file_upload", "attr", "attr_translation_parameters", "auto_initialize", "block_name", "block_prefix", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "help_attr", "help_html", "help_translation_parameters", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "label_translation_parameters", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".

mardon
  • 1,065
  • 1
  • 11
  • 29

1 Answers1

1

The problem is in your configureOptions(). Add a new index student to it.

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => StudentCourseNewDetails::class,
            'student' => null
        ]);
    }
}
Caconde
  • 4,177
  • 7
  • 35
  • 32
  • 1
    StudentCourseNewDetails is class as Data tranform object fot StudentCourse entity – mardon Sep 06 '19 at 16:45
  • You are right, but in code is another problem in createForm mus be $user->getId() not user as object – mardon Sep 06 '19 at 17:04
  • 1
    It should work if you do `$form = $this->createForm(NewStudentCourseType::class, $scnd, ['student' => $user->getId()]);`. Are you getting any errors? – Caconde Sep 06 '19 at 17:09
  • yes this code is working, but when form is submited StudentCourseNewDetails instance have in student id but i need user object – mardon Sep 06 '19 at 17:25
  • 1
    Ok. That new issue seems to be a bit out of the scope of this question. Take a look at [this answer](https://stackoverflow.com/a/27095322/2463836). I believe it solves the same problem you are facing. If not, add a new question, just to keep the scope of this one. – Caconde Sep 06 '19 at 17:49