提交表单时Symfony2 Undefined Index Parent

时间:2012-07-24 23:54:49

标签: php doctrine twig symfony-2.1

Symfony 2.1 Beta 4

当我在我的twig模板中渲染我的表单时,如果我尝试使用form_widget或form_rest,我将收到此错误 - “在呈现模板期间抛出异常(”注意:未定义的索引:parent in MySite / vendor / symfony / symfony / src / Symfony / Component / Form / FormView.php 308“)。”我能够使用form_row手动构建表单并更新属性,但不包括csrf标记。

编辑7/27 看起来当我只包含PropertyType表单类时,form_rest工作正常。一旦我包含AddressType,我就会收到上述错误。

我正在渲染一个具有嵌入形式的地址实体的属性(即房屋)实体。

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);
}

我的PropertyType表单类:

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';
}
}

编辑7/27:这是我的AddressType类:

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';
}
}

2 个答案:

答案 0 :(得分:3)

我在Github https://github.com/symfony/symfony/issues/5121上发现了这个问题讨论,果然,在我的php.ini文件中禁用twig扩展解决了这个问题。

答案 1 :(得分:0)

Spike解决方案适用于使用{{ form_widget(form._token) }}

呈现令牌输入