0

I have this code in my controller:

/**
 * Displays a form to create a new Bank Account
 *
 * @Route("/account/new", name="wba_new")
 * @Method("GET")
 * @Template("BankBundle:BankAccount:new.html.twig")
 */
public function newBankAccountAction() {
    $entity = new Account();
    $form = $this->createForm(new AccountType(), $entity);

    return array('entity' => $entity, 'form' => $form->createView());
}

/**
 * Handle bank account creation
 *
 * @Route("/", name="wba_create")
 * @Method("POST")
 */
public function createAction(Request $request) {
    $entity = new Account();
    $form = $this->createForm(new AccountType(), $entity);
    $form->handleRequest($request);

    print_r($request);
    exit;

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

        return $this->redirect($this->generateUrl('wba_list'));
    }

    return array('entity' => $entity, 'form' => $form->createView());
}

When I call /account/new the form is showed without any problems and action goes to / but when I send the form I got this error:

The controller must return a response (Array(entity => Object(BankBundle\Entity\AccountType), form => Object(Symfony\Component\Form\FormView)) given).

Why? What is wrong in my code?

UPDATE

I found where the issue was, I have two routes with the same definition in two different controllers:

/**
 * Handle bank account creation
 *
 * @Route("/", name="wba_create")
 * @Method("POST")
 */

After fix the problem things works

Reynier
  • 2,420
  • 11
  • 51
  • 91
  • 1
    Isn't the controller action supposed to return a `Response` object of some kind? – Joachim Isaksson Aug 05 '13 at 18:44
  • @JoachimIsaksson not always, see my edition – Reynier Aug 05 '13 at 18:48
  • 1
    @JoachimIsaksson is right. You forgot the `@Template` annotation on top of your `createAction()` function. – cheesemacfly Aug 05 '13 at 18:54
  • A controller should always return a response. Template annotation makes that a template is rendered and a response is built (and eventually returned). – Jakub Zalas Aug 05 '13 at 19:42
  • please add your solution ( having two routes with the same definition ) as an answer and accept it so the question is marked as resolved. While this was the solution in your case ... i guess 95% of the people facing this problem have forgotten SensioFramworkExtraBundle's `@Template` annotation or forgot to activate FOSRestBundle's view response listener ... or any other listener converting data returned by a controller action into a response object automatically. – Nicolai Fröhlich Aug 06 '13 at 02:42
  • @nifr I added but can't accept until two days so I'll wait and come back to accept the answer – Reynier Aug 06 '13 at 02:52

2 Answers2

0

After read the code complete again and try to find where my mistake was, finally I found. I had two controllers: AccountController.php and TestController.php and in both I had defined (my mistake because I just copied AccountController.php to TestController.php) the same routes as in this function:

/**
 * Handle bank account creation
 *
 * @Route("/", name="wba_create")
 * @Method("POST")
 */
public function createAction(Request $request) {
    ...
}

For that reason, I tough, was why the data was lost when Symfony tries to call the route wba_create. I didn't add the annotation @Template(""). That's the solution, hope works for any running the same problem

Reynier
  • 2,420
  • 11
  • 51
  • 91
0
/**
 * Displays a form to create a new Bank Account
 *
 * @Route("/account/new", name="wba_new")
 */
public function newBankAccountAction()
{
    $entity = new Account();
    $form = $this->createForm(new AccountType(), $entity);

    return $this->render('BankBundle:BankAccount:new.html.twig',array(
            'entity' => $entity,
            'form' => $form->createView(),
    ));
}
Wing
  • 44
  • 3