Zend framework2 满足空值或某些其他条件时验证筛选器

Zend framework2 满足空值或某些其他条件时验证筛选器,zend-framework2,Zend Framework2,Im在Zend2上使用此输入过滤器: $this->get('foo')->getValidatorChain()->attach( new GreaterThan(array( 'min' => 1, 'inclusive' => true )) ); 这是可行的,但我知道我还需要允许foo var为空,因此如果为空,它将进行验证,如果不为空,它将对值应用Greate

Im在Zend2上使用此输入过滤器:

    $this->get('foo')->getValidatorChain()->attach(
        new GreaterThan(array(
            'min' => 1,
            'inclusive' => true
        ))
    );
这是可行的,但我知道我还需要允许foo var为空,因此如果为空,它将进行验证,如果不为空,它将对值应用GreaterThan条件

我试图通过添加来解决这个问题:
$this->get('foo')->setAllowEmpty(true)在该代码之上,但不起作用,它将继续验证GreaterThan条件


如何解决这个问题?

我会在控制器内的post之后和验证之前连接验证程序。您也可以向表单类中添加一个方法,但出于演示目的,我尽量使其简短,但在表单中这样做可能会更干净

...
if($request->isPost()) {
   $data = $request->getpost();
   if(!empty($data['foo'])) {
     $form->get('foo')->getValidatorChain()->attach(
        new GreaterThan(array(
        'min' => 1,
        'inclusive' => true ))
     );
   }
   if($form->isValid()) {
      //your code
   }
}
...

也可以创建自己的验证器。docu有几个例子,并且非常清楚如何使用它们

您能说明一下这个
是什么类以及它扩展了什么吗?