Jaeger
Jaeger

Reputation: 1754

Unable to pass a specific form in the view

I have created a CRUD with Symfony 3 that allows me to create different missions with a few specificities. I want to to create a function that allows someone with a specific role to change a mission's status just by clicking a button, that would be shown in the view like this

{{form_start(missionInProgress) }}
    <input type="submit" value="Submit" />
{{form_end(missionInProgress) }}

Since I'm a real newbie and I can't find concrete example on Google, I tried a lot of things, but none worked so far. I tried to create a public function that would modify the mission's status when someone clicks on the input button

public function that updates the mission's status:

/**
 * @Route("/{id}/encours", name="mission_encours")
 * @Security has_role('ROLE_RECRUTEUR')
 * @Method("POST")
 */
public function enCoursAction(Request $request, Mission $mission){
    $form = $this->missionInProgress($mission);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $em = $this->getDoctrine()->getManager();
        $mission->setStatut("En cours");
        $em->persist($mission);

    }
}

And I also tried to create a private function like the one that allows a mission to be deleted from anywhere.

**Private function that calls the public function: **

/**
 * @param Mission $mission
 * @Security has_role('ROLE_RECRUTEUR')
 * @return \Symfony\Component\Form\Form The form
 */
private function missionInProgress(Mission $mission){
    $this->createFormBuilder()
        ->setAction($this->generateUrl('mission_encours', array('id' => $mission->getId())))
        ->setMethod('POST')
        ->getForm();
}

Following the "createDeleteForm" example, I implemented it in the showAction as follow:

/**
 * Finds and displays a Mission entity.
 *
 * @Route("/{id}", name="mission_show")
 * @Method("GET")
 */
public function showAction(Mission $mission)
{
    $deleteForm = $this->createDeleteForm($mission);
    $enCours = $this->missionInProgress($mission); /* There */

    return $this->render('mission/show.html.twig', array(
        'mission' => $mission,
        'delete_form' => $deleteForm->createView(),
        'missionInProgress' => $enCours->createView(), /* And there */
    ));
}

But when I try to see the result, I get the following error:

Error: Call to a member function createView() on null

Obviously nothing gets inside missionInProgress(), but I can't figure why and how to make this damn thing work. I also don't think that everything I did was necessary, but I thought that if I do this, I might increase my success chances...

Anyone has an idea ?

Thank you in advance

Upvotes: 0

Views: 37

Answers (1)

doydoy44
doydoy44

Reputation: 5772

Try to add returnin your missionInProgress() method

Upvotes: 1

Related Questions