Symfony的2.1 Beta 4的Symfony2中未定義指數父呈現形式
當我呈現在我的樹枝模板我的形式,如果我嘗試使用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);
}
我的屬性類型的表單類:
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:這是我的地址類型類:
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引發異常聽起來不正確。你應該能夠使用這些如果形式正確構建 – mask8 2012-07-25 00:09:24
好吧,我現在還沒有實現我的實體的所有領域,因爲我想從小開始。 – mhoff 2012-07-25 12:33:37
你有自定義的表單字段叫做AddressType,你有沒有在類中正確實現getParent()函數?您是否爲自定義字段創建了小枝小部件模板?我也認爲'FormBuilderInterface'應該是'FormBuilder' – mask8 2012-07-25 17:19:10