2013-10-23 60 views
0

我不明白。這是我的字段集:ZF2 - 表單中的元素未找到

<?php 
namespace example\Form; 

use example\Entity\User; 
use example\Entity\Strategy\PasswordStrategy; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\Form\Fieldset; 
use Zend\InputFilter\InputFilterProviderInterface; 

class UserFieldset extends Fieldset implements InputFilterProviderInterface 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct('user'); 

     $this->add(array(
      'name' => 'id', 
      'type' => 'Zend\Form\Element\Hidden', 
     )); 

    } 

    public function getInputFilterSpecification() 
    { 
     return array(); 
    } 
} 

這是我的形式:

<?php 
namespace example\Form; 

use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\Form\Form; 
use example\Form\UserFieldset; 

class UserForm extends Form 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct('user-form'); 

     $userFieldset = new UserFieldset($objectManager); 
     $userFieldset->setUseAsBaseFieldset(true); 

     $this->add($userFieldset); 

     $this->add(array(
      'name' => 'submit', 
      'type' => 'Zend\Form\Element\Submit', 
      'attributes' => array(
       'value' => 'Go', 
       'id' => 'submitbutton', 
      ), 
     )); 
    } 
} 

我的行動:

public function userAction() 
{ 
    $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 
    $userForm = new UserForm($objectManager); 
    return array(
     'form' => $userForm 
    ); 
} 

我的觀點:

<?php 
$form = $this->form; 
$form->setAttribute('action', $this->url('user')); 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formRow($form->get('id')); 
echo $this->formRow($form->get('submit')); 
echo $this->form()->closeTag(); 

而且我的錯誤:

以表格形式發現[id]的名稱中沒有元素

爲什麼?只需提交領域呈現不錯,但只要我添加id字段我得到一個錯誤......

回答

1

可以渲染使用的FormCollection()

<?php 
$form = $this->form; 
$form->setAttribute('action', $this->url('user')); 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formCollection($form->get('user')); 
echo $this->form()->closeTag(); 

或者這樣

<?php 
$form = $this->form; 
$form->setAttribute('action', $this->url('user')); 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formRow($form->get('user')->get("id")); 
echo $this->formRow($form->get('user')->get("submit")); 
echo $this->form()->closeTag(); 
1

嘗試

echo $this->formRow($form->get('user')->get('id')); 

您需要通過字段集得到這個元素。