symfony4 inject parameter in form












0















I have a Business Entity and a BusinessObject Entity, and I would like to link the BusinessObject to the current Business when I create a new BusinessObject.
For example, if my route is business/{id}/object/new, I would like to have the object related with the Business (thanks to the id).



In my BusinessObject Controller, I managed to use @ParamConverter to get the Business id.
In my BusinessObject Form, I put an HiddenType to my business entry because I don't want it to appear, and set data to business_ID.
I struggle in configureOptions to get the business ID, I can't figure out how to get the business id from here.



BusinessObject Controller (route new):



/**
* @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
* @ParamConverter("business", options={"id" = "post_id"})
*/
public function new(Request $request,Business $business): Response
{
$businessObject = new BusinessObject();

$businessID = $business->getId();

$form = $this->createForm(BusinessObjectType::class, $businessObject,array(
'business_ID'=>$businessID,
));
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($businessObject);
$em->flush();

return $this->redirectToRoute('business_object_index');
}

return $this->render('business_object/new.html.twig', [
'business_object' => $businessObject,
'business'=>$business,
'form' => $form->createView(),
]);
}


BusinessObjectType:



class BusinessObjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('object',TextType::class)
->add('complement')
->add('status')
->add('durationExpected')
->add('durationAchieved')
->add('client')
->add('projectManager')
->add('business',HiddenType::class,array(
'data' => $options['business_ID']

))
;
}

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


With this code, I get an error Expected argument of type "AppEntityBusiness or null", "string" given. I think this have something to do with the function configureOptions() in my Form










share|improve this question



























    0















    I have a Business Entity and a BusinessObject Entity, and I would like to link the BusinessObject to the current Business when I create a new BusinessObject.
    For example, if my route is business/{id}/object/new, I would like to have the object related with the Business (thanks to the id).



    In my BusinessObject Controller, I managed to use @ParamConverter to get the Business id.
    In my BusinessObject Form, I put an HiddenType to my business entry because I don't want it to appear, and set data to business_ID.
    I struggle in configureOptions to get the business ID, I can't figure out how to get the business id from here.



    BusinessObject Controller (route new):



    /**
    * @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
    * @ParamConverter("business", options={"id" = "post_id"})
    */
    public function new(Request $request,Business $business): Response
    {
    $businessObject = new BusinessObject();

    $businessID = $business->getId();

    $form = $this->createForm(BusinessObjectType::class, $businessObject,array(
    'business_ID'=>$businessID,
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $em->persist($businessObject);
    $em->flush();

    return $this->redirectToRoute('business_object_index');
    }

    return $this->render('business_object/new.html.twig', [
    'business_object' => $businessObject,
    'business'=>$business,
    'form' => $form->createView(),
    ]);
    }


    BusinessObjectType:



    class BusinessObjectType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('object',TextType::class)
    ->add('complement')
    ->add('status')
    ->add('durationExpected')
    ->add('durationAchieved')
    ->add('client')
    ->add('projectManager')
    ->add('business',HiddenType::class,array(
    'data' => $options['business_ID']

    ))
    ;
    }

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


    With this code, I get an error Expected argument of type "AppEntityBusiness or null", "string" given. I think this have something to do with the function configureOptions() in my Form










    share|improve this question

























      0












      0








      0








      I have a Business Entity and a BusinessObject Entity, and I would like to link the BusinessObject to the current Business when I create a new BusinessObject.
      For example, if my route is business/{id}/object/new, I would like to have the object related with the Business (thanks to the id).



      In my BusinessObject Controller, I managed to use @ParamConverter to get the Business id.
      In my BusinessObject Form, I put an HiddenType to my business entry because I don't want it to appear, and set data to business_ID.
      I struggle in configureOptions to get the business ID, I can't figure out how to get the business id from here.



      BusinessObject Controller (route new):



      /**
      * @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
      * @ParamConverter("business", options={"id" = "post_id"})
      */
      public function new(Request $request,Business $business): Response
      {
      $businessObject = new BusinessObject();

      $businessID = $business->getId();

      $form = $this->createForm(BusinessObjectType::class, $businessObject,array(
      'business_ID'=>$businessID,
      ));
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
      $em = $this->getDoctrine()->getManager();
      $em->persist($businessObject);
      $em->flush();

      return $this->redirectToRoute('business_object_index');
      }

      return $this->render('business_object/new.html.twig', [
      'business_object' => $businessObject,
      'business'=>$business,
      'form' => $form->createView(),
      ]);
      }


      BusinessObjectType:



      class BusinessObjectType extends AbstractType
      {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('object',TextType::class)
      ->add('complement')
      ->add('status')
      ->add('durationExpected')
      ->add('durationAchieved')
      ->add('client')
      ->add('projectManager')
      ->add('business',HiddenType::class,array(
      'data' => $options['business_ID']

      ))
      ;
      }

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


      With this code, I get an error Expected argument of type "AppEntityBusiness or null", "string" given. I think this have something to do with the function configureOptions() in my Form










      share|improve this question














      I have a Business Entity and a BusinessObject Entity, and I would like to link the BusinessObject to the current Business when I create a new BusinessObject.
      For example, if my route is business/{id}/object/new, I would like to have the object related with the Business (thanks to the id).



      In my BusinessObject Controller, I managed to use @ParamConverter to get the Business id.
      In my BusinessObject Form, I put an HiddenType to my business entry because I don't want it to appear, and set data to business_ID.
      I struggle in configureOptions to get the business ID, I can't figure out how to get the business id from here.



      BusinessObject Controller (route new):



      /**
      * @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
      * @ParamConverter("business", options={"id" = "post_id"})
      */
      public function new(Request $request,Business $business): Response
      {
      $businessObject = new BusinessObject();

      $businessID = $business->getId();

      $form = $this->createForm(BusinessObjectType::class, $businessObject,array(
      'business_ID'=>$businessID,
      ));
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
      $em = $this->getDoctrine()->getManager();
      $em->persist($businessObject);
      $em->flush();

      return $this->redirectToRoute('business_object_index');
      }

      return $this->render('business_object/new.html.twig', [
      'business_object' => $businessObject,
      'business'=>$business,
      'form' => $form->createView(),
      ]);
      }


      BusinessObjectType:



      class BusinessObjectType extends AbstractType
      {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('object',TextType::class)
      ->add('complement')
      ->add('status')
      ->add('durationExpected')
      ->add('durationAchieved')
      ->add('client')
      ->add('projectManager')
      ->add('business',HiddenType::class,array(
      'data' => $options['business_ID']

      ))
      ;
      }

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


      With this code, I get an error Expected argument of type "AppEntityBusiness or null", "string" given. I think this have something to do with the function configureOptions() in my Form







      php forms symfony twig symfony4






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 21:53









      nicoolaslbnicoolaslb

      2216




      2216
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The approach can be:



          public function new(Request $request,Business $business): Response
          {
          $businessObject = new BusinessObject();

          $form = $this->createForm(BusinessObjectType::class, $businessObject);
          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          // I suppose your setter is `setBusiness`, otherwise use more suitable one
          $businessObject->setBusiness($business);
          $em = $this->getDoctrine()->getManager();
          $em->persist($businessObject);
          $em->flush();


          Form builder is:



          builder
          ->add('object',TextType::class)
          ->add('complement')
          ->add('status')
          ->add('durationExpected')
          ->add('durationAchieved')
          ->add('client')
          ->add('projectManager'); // No business field


          Another option is to embed BusinessType form into BusinessObjectType, you can read more about form embedding here.






          share|improve this answer
























          • Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

            – nicoolaslb
            Jan 3 at 14:34












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013662%2fsymfony4-inject-parameter-in-form%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The approach can be:



          public function new(Request $request,Business $business): Response
          {
          $businessObject = new BusinessObject();

          $form = $this->createForm(BusinessObjectType::class, $businessObject);
          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          // I suppose your setter is `setBusiness`, otherwise use more suitable one
          $businessObject->setBusiness($business);
          $em = $this->getDoctrine()->getManager();
          $em->persist($businessObject);
          $em->flush();


          Form builder is:



          builder
          ->add('object',TextType::class)
          ->add('complement')
          ->add('status')
          ->add('durationExpected')
          ->add('durationAchieved')
          ->add('client')
          ->add('projectManager'); // No business field


          Another option is to embed BusinessType form into BusinessObjectType, you can read more about form embedding here.






          share|improve this answer
























          • Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

            – nicoolaslb
            Jan 3 at 14:34
















          1














          The approach can be:



          public function new(Request $request,Business $business): Response
          {
          $businessObject = new BusinessObject();

          $form = $this->createForm(BusinessObjectType::class, $businessObject);
          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          // I suppose your setter is `setBusiness`, otherwise use more suitable one
          $businessObject->setBusiness($business);
          $em = $this->getDoctrine()->getManager();
          $em->persist($businessObject);
          $em->flush();


          Form builder is:



          builder
          ->add('object',TextType::class)
          ->add('complement')
          ->add('status')
          ->add('durationExpected')
          ->add('durationAchieved')
          ->add('client')
          ->add('projectManager'); // No business field


          Another option is to embed BusinessType form into BusinessObjectType, you can read more about form embedding here.






          share|improve this answer
























          • Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

            – nicoolaslb
            Jan 3 at 14:34














          1












          1








          1







          The approach can be:



          public function new(Request $request,Business $business): Response
          {
          $businessObject = new BusinessObject();

          $form = $this->createForm(BusinessObjectType::class, $businessObject);
          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          // I suppose your setter is `setBusiness`, otherwise use more suitable one
          $businessObject->setBusiness($business);
          $em = $this->getDoctrine()->getManager();
          $em->persist($businessObject);
          $em->flush();


          Form builder is:



          builder
          ->add('object',TextType::class)
          ->add('complement')
          ->add('status')
          ->add('durationExpected')
          ->add('durationAchieved')
          ->add('client')
          ->add('projectManager'); // No business field


          Another option is to embed BusinessType form into BusinessObjectType, you can read more about form embedding here.






          share|improve this answer













          The approach can be:



          public function new(Request $request,Business $business): Response
          {
          $businessObject = new BusinessObject();

          $form = $this->createForm(BusinessObjectType::class, $businessObject);
          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          // I suppose your setter is `setBusiness`, otherwise use more suitable one
          $businessObject->setBusiness($business);
          $em = $this->getDoctrine()->getManager();
          $em->persist($businessObject);
          $em->flush();


          Form builder is:



          builder
          ->add('object',TextType::class)
          ->add('complement')
          ->add('status')
          ->add('durationExpected')
          ->add('durationAchieved')
          ->add('client')
          ->add('projectManager'); // No business field


          Another option is to embed BusinessType form into BusinessObjectType, you can read more about form embedding here.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 8:31









          u_mulderu_mulder

          37.5k53148




          37.5k53148













          • Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

            – nicoolaslb
            Jan 3 at 14:34



















          • Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

            – nicoolaslb
            Jan 3 at 14:34

















          Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

          – nicoolaslb
          Jan 3 at 14:34





          Thats's exactly what I was looking for, thanks ! In a normal way I would have just embedded it but the fact here is that my businessObject needed to have a particular value, thanks again !

          – nicoolaslb
          Jan 3 at 14:34




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013662%2fsymfony4-inject-parameter-in-form%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]