簡體   English   中英

我如何設置inputFilter在zend Framework 2中不允許空白?

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

我如何設置inputFilter在zend Framework 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']
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM