KaSkuLL
KaSkuLL

Reputation: 551

get value from controller to form 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".

Upvotes: 1

Views: 200

Answers (1)

habibun
habibun

Reputation: 1630

declare an empty array in setDefaultOptions.

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

Upvotes: 1

Related Questions