2

我是 zend 框架 2 的新手,我有一个关于比较工厂支持形式的两个输入的问题。我的情况如下:

我想比较两个输入,例如$startDate$endDate。我想验证它$startDate总是小于$endDate. 我将如何做到这一点?例如:

$inputFilter->add($factory->createInput(array(
                'name'     => 'startDate',
                'required' => true,
                'validators' => array(
                    array(
                        'name'    => 'LessThan',
                        'options' => array(
                            'max'      => $endDate,
                        ),
                    ),
                ),
            )));

仅供参考,我正在关注专辑教程,并且$inputFilter是在classTable.php.

谢谢

4

2 回答 2

4

感谢脆!我用类似的方法解决了它:

$inputFilter->add($factory->createInput(array(
            'name'     => 'startDate',
            'required' => true,
            'name'     => 'Callback',
                'options' => array(
                    'message' => array( 
                        Callback::INVALID_VALUE => 'Invalid period is given.',
                    ),
                    'callback' => function($value, $context=array()) {
                        return $value < $context['endDate'];
                    },
                ),
            )));
于 2013-07-26T16:59:15.087 回答
0

The above answer probably be correct, but there might be some syntax or Callback error might be occur. The reason is, We generally use Callback Validation function in Models InputFilters, Not in a forms definition section(as of Zend Framework version 2.2.1).

This call back script part should come inside Model - InputFilters, Please refer this link: https://stackoverflow.com/a/19263037/2190889

As per this Url reference, date validation part works perfectly.

于 2013-10-09T04:26:15.573 回答