Symfony2呈现窗体时未定义的索引父级


Symfony2 Undefined Index Parent when rendering a Form

Symfony 2.1 Beta 4

当我在我的小树枝模板中呈现我的表单时,如果我尝试使用form_widget或form_rest,我会收到这个错误-"在呈现模板的过程中引发了异常("注意:未定义的索引:MySite/vvendor/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';
}
}

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

Spike解决方案将使用{{ form_widget(form._token) }} 渲染令牌输入