这可能是您的用户实体(缩写版本)的实体(和表单定义):
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as Form;
/**
* @ORM\Entity
* @ORM\Table(name="application_user")
* @Form\Name("user")
* @Form\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
*/
class User
{
/**
* @var int
* @ORM\Id @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue
* @Form\Exclude()
*/
protected $id;
/**
* @var string
* @ORM\Column(name="user_name", type="string", length=255, nullable=false)
* @Form\Filter({"name":"StringTrim"})
* @Form\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @Form\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
* @Form\Attributes({"type":"text"})
* @Form\Options({"label":"Username:"})
*/
protected $username;
/**
* @var string
* @ORM\Column(name="email", type="string", length=90, unique=true)
* @Form\Type("Zend\Form\Element\Email")
* @Form\Options({"label":"Your email address:"})
*/
protected $email;
}
并使用这种形式:
use Zend\Form\Annotation\AnnotationBuilder;
$builder = new AnnotationBuilder();
$form = $builder->createForm('Application\Entity\User');
// Also possible:
// $form = $builder->createForm(new Application\Entity\User());
因此构建器需要定义类的完全限定名称。您使用注释设置的名称是表单的名称,例如用于创建表单的 id 属性。
如果你有一个 use 语句,你也可以绑定命名空间:
use Zend\Form\Annotation\AnnotationBuilder;
use Application\Entity\User;
$builder = new AnnotationBuilder();
$form = $builder->createForm('User');
// Also possible:
// $form = $builder->createForm(new User());