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 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题