0

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';
}
}
j0k
  • 22,600
  • 28
  • 79
  • 90
mhoff
  • 31
  • 1
  • 10
  • 2
    Could we see PropertyType class? I think that form_widget or form_rest throwing exceptions doesn't sound right in the first place. You should be able to use those if form is built properly – mask8 Jul 25 '12 at 00:09
  • Ok, I haven't implemented all of the fields of my entity at this point because I wanted to start small. – mhoff Jul 25 '12 at 12:33
  • You have custom form field called `AddressType`, have you implemented `getParent()` function properly in the class? Have you created twig widget template for the custom field? also I think `FormBuilderInterface` should be `FormBuilder` – mask8 Jul 25 '12 at 17:19
  • I'm not sure what to do with the getParent method? Do I have to specify the AddressType as the parent? – mhoff Jul 27 '12 at 22:00

2 Answers2

3

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.

mhoff
  • 31
  • 1
  • 10
0

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

Vitalii Zurian
  • 17,858
  • 4
  • 64
  • 81