【问题标题】:Symfony2 Undefined Index Parent when rendering a FormSymfony2 Undefined Index Parent 渲染表单时
【发布时间】:2012-07-23 08:46:47
【问题描述】:

Symfony 2.1 Beta 4

当我在我的 twig 模板中渲染我的表单时,如果我尝试使用 form_widget 或 form_rest,我将收到此错误 - “在渲染模板期间引发了异常(”注意:未定义索引:父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';
}
}

【问题讨论】:

  • 我们能看到 PropertyType 类吗?我认为 form_widget 或 form_rest 抛出异常听起来并不正确。如果表单构建正确,您应该能够使用它们
  • 好的,此时我还没有实现我的实体的所有字段,因为我想从小处着手。
  • 你有一个名为AddressType的自定义表单字段,你在课堂上正确实现了getParent()功能吗?您是否为自定义字段创建了树枝小部件模板?我也认为FormBuilderInterface 应该是FormBuilder
  • 我不确定如何处理 getParent 方法?我必须将 AddressType 指定为父级吗?

标签: php doctrine twig symfony-2.1


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    Spike 解决方案是使用 {{ form_widget(form._token) }} 呈现令牌输入

    【讨论】:

    • 这确实有效,让我可以保存。虽然不是一个理想的解决方案。
    • 同意。只是为了不推迟部署并赢得更多时间进行调查;)
    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2013-05-20
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多