mhoff
mhoff

Reputation: 31

Symfony2 Undefined Index Parent when rendering a Form

Symfony 2.1 Beta 4

When I am rendering my form in my twig template, if I try to use form_widget or form_rest, I will receive this error - "An exception has been thrown during the rendering of a template ("Notice: Undefined index: parent in MySite/vendor/symfony/symfony/src/Symfony/Component/Form/FormView.php line 308")." I am able to manually build the form using form_row and update the property but that does not include the csrf token.

EDIT 7/27 It looks like when I ONLY include the PropertyType form class, form_rest works fine. Once I include AddressType, I get the above error.

I am rendering a property (i.e. a house) entity with an embedded form of an address entity.

public function showAction( $property_id )
{
    $em = $this->getDoctrine()->getEntityManager();
    $property = $em->getRepository('MySystemBundle:Property')->find( $property_id );

    if( !$property ){
        throw $this->createNotFoundException('The requested property does not exist.');
    }       

    $form = $this->createForm(new PropertyType(), $property);

    $request = $this->get('request');

    if( $request->getMethod() == 'POST' ){
        $form->bind($request);

        if( $form->isValid() ){
            $em->persist($property);
            $em->flush();

            $this->get('session')->setFlash('notice', 'Your changes were saved!');
            return $this->redirect($this->generateUrl('system_propertyShow', 
                array('property_id' => $property_id)));
        }
    }

    $vars['property'] = $property;
    $vars['form'] = $form->createView();

    return $this->render('MySystemBundle:Property:show.html.twig', $vars);
}

My PropertyType form class:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PropertyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('purchaseAmount', 'text', array('label' => 'Purchase Amount', 'required' => false))
        ->add('depositAmount', 'text', array('label' => 'Deposit Amount', 'required' => false))
        ->add('additionalCostsAmount', 'text', array('label' => 'Additional Costs', 'required' => false))
        ->add('balanceAmount', 'text', array('label' => 'Balance', 'required' => false))
        ->add('dateBalanceDue', 'datetime', array('label' => 'Balance Due', 'widget' => 'single_text', 'required' => false))
        ->add('squareFootage', 'number', array('label' => 'Square Footage', 'required' => false))
        ->add('isOccupied', 'choice', array(
            'label' => 'Is Occupied?',
            'choices' => array('1' => 'Yes', '0' => 'No'),
            'required' => false))
        ->add('address', new AddressType());
}

public function getDefaultOptions(array $options)
{
    return array('data_class' => 'MySite\Bundle\SystemBundle\Entity\Property');
}

public function getName()
{
    return 'property';
}
}

EDIT 7/27: Here is my AddressType class:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('line1', 'text', array('label' => 'Line 1'))
        ->add('line2', 'text', array('label' => 'Line 2', 'required' => false))
        ->add('line3', 'text', array('label' => 'Line 3', 'required' => false))
        ->add('city', 'text', array('label' => 'City'))
        ->add('township', 'text', array('label' => 'Township', 'required' => false))
        ->add('zipcode', 'text', array('label' => 'Zip'))
        ->add('state', new StateType());
}

public function getDefaultOptions(array $options)
{
    return array('data_class' => 'MySite\Bundle\SystemBundle\Entity\Address');
}

public function getName()
{
    return 'address';
}
}

Upvotes: 0

Views: 3521

Answers (2)

mhoff
mhoff

Reputation: 31

I discovered this issue discussion on Github https://github.com/symfony/symfony/issues/5121 and sure enough, disabling the twig extension in my php.ini file resolved the issue.

Upvotes: 3

Vitalii Zurian
Vitalii Zurian

Reputation: 17986

Spike solution for you would be to render token input with {{ form_widget(form._token) }}

Upvotes: 0

Related Questions