【问题标题】:ZF2 Annotation builded form not working - all fields excludedZF2 注释构建的表单不起作用 - 排除所有字段
【发布时间】:2014-06-11 12:09:38
【问题描述】:

我正在使用 Zend Framework 2.3.1 ( PHP 5.4.21 ) 我正在尝试使用注释构建表单,我使用的代码很简单:

控制器:

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\Test;
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
// ...
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
/* @var $entityManager \Doctrine\ORM\EntityManager */
$test = new Test();
$builder = new AnnotationBuilder($entityManager);
// for debugging purposes
$spec = $builder->getFormSpecification($test);
\Zend\Debug\Debug::dump($spec);

Application\Entity\Test 看起来像这样:

namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;

/**
 * @Annotation\Name("this-works")
 * @ORM\Entity 
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 */
class Test{


/** 
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @Annotation\Attributes({"type":"hidden"})
*/
protected $id;


/** 
* 
* @ORM\Column(type="string") 
* @Annotation\Filter({"name":"StringTrim"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @Annotation\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
* @Annotation\Attributes({"type":"text"})
* @Annotation\Options({"label":"Username:"})
* @Annotation\AllowEmpty(true)
*/  
protected $fullName;


}

我得到的结果是:

object(ArrayObject)#338 (1) {
  ["storage":"ArrayObject":private] => array(6) {
    ["name"] => string(4) "this-works"
    ["attributes"] => array(0) {
    }
    ["elements"] => array(0) {
    }
    ["fieldsets"] => array(0) {
    }
    ["hydrator"] => string(35) "Zend\Stdlib\Hydrator\ObjectProperty"
    ["input_filter"] => object(ArrayObject)#339 (1) {
      ["storage":"ArrayObject":private] => array(0) {
      }
    }
  }
}

表单名称注释正在工作,但所有字段都被排除在外。

我试图调试它,我发现如果我从(注意删除回调)更改 Zend/Form/Annotation/AnnotationBuilder.php 函数 checkForExclude

protected function checkForExclude($annotations)
{    

    $results = $this->getEventManager()->trigger('checkForExclude', $this, array(
        'annotations' => $annotations,
    ), function ($r) { 
        return (true === $r);
    });

    return (bool) $results->last();
}

protected function checkForExclude($annotations)
{    

    $results = $this->getEventManager()->trigger('checkForExclude', $this, array(
        'annotations' => $annotations,
    ));

    return (bool) $results->last();
}

它工作正常(fullName 字段在表单声明中)。

我做错了什么还是只是 ZF2 中的错误? 现在想弄清楚这几天,但我没有想法。

【问题讨论】:

    标签: php forms annotations zend-framework2


    【解决方案1】:

    到目前为止,我唯一的解决方案是在使用带注释的表单时从 application.config.php 中删除 ZendDeveloperTools。

    已知错误:https://github.com/zendframework/zf2/issues/6166

    【讨论】:

    • 请注意:此问题已在 ZF2 错误迁移期间关闭。如果它仍然存在,则需要重新打开它。
    【解决方案2】:

    您可能有一些东西干扰了 EventManager 的侦听器链。

    例如,如果我启用了 ZendDeveloperTools 模块,我会得到相同的结果。您可以通过检查传递给 Zend\EventManager\EventManager.php:464 中事件回调闭包的最终结果来验证这一点

    $listenerCallback = $listener->getCallback();
    if ('checkForExclude' === $event) {
        var_dump($listenerCallback);
    }
    

    如果回调的类不是 Zend\Form\Annotation\ElementAnnotationsListener,那么闭包将不会接收正确的结果作为参数。

    【讨论】:

      【解决方案3】:

      幸运的是,您可以保留 ZendDeveloperTools!您只需将位于 ApplicationPath/config/autoload/zdt.local.php 上的 ZDT 自动加载文件中的一个属性更改为 false:

          array('Events' => array('Enabled' = false, ...), ...)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        相关资源
        最近更新 更多