从控制器获取值以形成symfony 3

时间:2017-11-25 12:35:59

标签: php forms symfony 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()
    ));
}

我的表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('idacademicprogram', EntityType::class, array(
            "required"=>"required",
            'class' => 'AppBundle:Academicprogram',
            'choice_label' => 'name'
        ))
        ->add('iduser', NumberType::class, array("required"=>"required",
            "data" =>$options["id"],
            "attr"=>array(
            "class" => "form-iduser form-control"
        )));
}

我收到此错误:

  

选项" id"不存在。定义的选项是:" action",   " allow_extra_fields"," attr"," auto_initialize"," block_name",   " by_reference","复合","约束"," csrf_field_name",   " csrf_message"," csrf_protection"," csrf_token_id",   " csrf_token_manager","数据"," data_class","禁用"," empty_data",   " error_bubbling"," error_mapping"," extra_fields_message",   " inherit_data"," invalid_message"," invalid_message_parameters",   "标签"," label_attr"," label_format","映射","方法",   " post_max_size_message"," property_path"," required",   " translation_domain"," trim"," upload_max_size_message",   " validation_groups"

1 个答案:

答案 0 :(得分:1)

在setDefaultOptions中声明一个空数组。

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'YourEntity',
        'id' => array(),
    ));
}