我如何将输入过滤器设置为不允许在 zend 框架 2 中留有空格


How I set the inputFilter to not allow white space in zend framework 2?

如何在 zend 框架 2 中将输入过滤器设置为不允许空格?
我正在尝试这个:

$inputFilter->add($factory->createInput(array(
            'name'     => 'codigo',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'not_empty',
                ),
            ),
            'filters' => array(
                 array(
                     'name' => 'Alnum',
                     'allowwhitespace' => false,
                 ),
            ),
        )));

代码中的一些点需要稍作调整;

  • ValidatorPluginManager 使用规范化别名来调用规范名称的验证者,这意味着"not_empty"不是有效的别名,它应该是"notempty"或"NotEmpty"。
  • 此外,您的 Alnum 过滤器签名似乎无效。你应该在带有下划线的 options 子键内提供其他选项。(是的,这真的很奇怪不一致)

试试这个:

$filter = new 'Zend'InputFilter'InputFilter();
$filter->add(array(
            'name'       => 'codigo',
            'required'   => true,
            'validators' => array(
                array(
                    'name' => 'NotEmpty',
                ),
            ),
            'filters' => array(
                 array(
                     'name'              => 'Alnum',
                     'options'           => array(
                        'allow_white_space' => false,
                    )
                 ),
            ),
        ));
$filter->setData(['codigo' => 'Whitespace exists']);
if($filter->isValid() === false) {
    // You'll fall here with a value like multiple spaces etc..
    var_dump($filter->getMessages());
} else {
    var_dump($filter->getValues()); // Prints ['codigo' => string 'Whitespaceexists']
}