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