注释表单不起作用

注释表单不起作用

本文介绍了Zend Framework 2 - 注释表单不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢我看着 AnnotationForms 这对于 ModelForms 来说似乎是最好的解决方案。但是,显示的示例对我不起作用。

Thanks to @Hikaru-Shindo I looked into AnnotationForms which seem to be the best available as a work-around for ModelForms. But the example shown here doesn't work for me.

use Zend\Form\Annotation\AnnotationBuilder;

$builder = new AnnotationBuilder();
$form    = $builder->createForm('User');

查看这段代码,我想知道 AnnotationBuilder 知道在哪里查找此用户表单。特别是因为在def形式的注释中有一个小写的'user'

Looking at this code I wonder where the AnnotationBuilder knows where to look for this user form. Especially because in the annotation in the form def there is a lowercase 'user'

@Annotation\Name("user")

我将表单def代码放入'MyModule / Form / UserForm.php'中,我的控制器。这是正确的方式吗?

I put the form def code into 'MyModule/Form/UserForm.php' and the lower code into my Controller. Is this the right way?

推荐答案

这可能是您的实体(和表单定义)为用户实体(短期版本):

This could be your entity (and form definition) for a user entity (shortend version):

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属性的表单名称。

So the builder needs the fully qualified name of your definition class.The name you set using the annotations is the name of the form used for example to create the form's id attribute.

如果您有一个use语句你也可以使用命名空间:

If you have a use statement for it you could also abond the namespace:

use Zend\Form\Annotation\AnnotationBuilder;
use Application\Entity\User;

$builder = new AnnotationBuilder();
$form    = $builder->createForm('User');
// Also possible:
// $form = $builder->createForm(new User());

这篇关于Zend Framework 2 - 注释表单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:36