我如何将inputFilter设置为不允许在zend框架2中使用空格? 我正在尝试这个:
$inputFilter->add($factory->createInput(array(
'name' => 'codigo',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
),
),
'filters' => array(
array(
'name' => 'Alnum',
'allowwhitespace' => false,
),
),
)));
答案 0 :(得分:1)
您的代码中的几点需要进行小的调整;
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']
}