0

I am just starting out with Symfony2 . Rather than having to learn twig, I decided to implement my forms in php instead. I followed the instructions in The Book, as follows:

in the controller:

public function editAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('MyBundle:MyEntity')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find MyEntity entity.');
    }

    $editForm = $this->createForm(new MyEntityType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    return $this->render('MyBundle:MyEntity:edit.html.php' array(
        'entity'      => $entity,
        'edit_form'    => $editForm->createView(),
        'delete_form'  => $deleteForm->createView()
    ));
}

and in edit.html.php:

<form action="<?php echo $view['router']->generate('myentity_update', array('id'=>$entity->getId()))?>" method="post" <?php echo $view['form']->enctype($editForm) ?>>

   <?php echo $view['form']->widget($editForm)?>

  <p>
    <button type="submit">Save Changes</button>
  </p>
</form>
<ul class="record_actions">
<li>
    <a href="<?php $view['router']->generate('myentity') ?>">
        Back to the list
    </a>
</li>
<li>
    <form action="<?php $view['router']->generate('myentity_delete', array('id'=>$entity->getId()) ) ?>" method="post">
        <?php echo $view['form']->widget($deleteForm)?>
        <button type="submit">Delete</button>
    </form>
</li>

when I try to display the form I get error: Notice: Undefined variable editForm

As far as I'm aware, I have followed the instructions exactly as they are given in The Book. I have tried replacing $editForm with $form in the action and template or replacing $editForm with edit_form in the template - needless to say, neither of these worked.

I would very much appreciate any suggestions as to what I should try next (or I may have to resign myself to learning twig, as at least the instructions for that seem to be correct).

3
  • can u please add the full code for both controller and html. Commented May 1, 2012 at 8:18
  • added all code from editAction function in controller and full code for the template Commented May 1, 2012 at 8:43
  • 1
    @richsage oops, sorry - done that now. not sure why I never noticed the tick mark next to the answers before Commented May 1, 2012 at 8:56

1 Answer 1

1

You pass your variable to your view as edit_form (the array keys are the names of the variables to use in the view):

return $this->render('MyBundle:MyEntity:edit.html.php' array(
    'entity'      => $entity,
    'edit_form'    => $editForm->createView(),
    'delete_form'  => $deleteForm->createView()
));

but you reference it in your view as $editForm:

<form action="<?php echo $view['router']->generate('myentity_update',
    array('id'=>$entity->getId()))?>" method="post" <?php echo $view['form']->enctype($editForm) ?>>

Decide on the same approach for both (edit_form vs editForm), and you should be good. Eg 'edit_form' becomes $edit_form in the view, and similarly for 'editForm' becoming $editForm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.