dougaopu7938 2017-11-25 12:35
浏览 37
已采纳

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

I don't know why i can't get the value on form from the variable of controller.

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', NumberType::class, array("required"=>"required",
            "data" =>$options["id"],
            "attr"=>array(
            "class" => "form-iduser form-control"
        )));
}

And i'm getting this error:

The option "id" does 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", "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", "upload_max_size_message", "validation_groups".

  • 写回答

1条回答 默认 最新

  • douwojiao5919 2017-11-25 15:50
    关注

    declare an empty array in setDefaultOptions.

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'YourEntity',
            'id' => array(),
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件