dongzhuang1923 2012-12-04 16:27
浏览 29
已采纳

ZF2未使用InputFilter验证输入

I am trying to create a single abstract class to manage input validation - in this class I have static functions that return InputFilterInterface objects that I can then add to my interface in the model class that handles the data. However, none of the data ever seems to be validated even though it is valid. I am not using Forms, by the way - the data is coming in via Ajax rather than an HTML form.

Here is my code:

Central Library:

abastract class InputFilters {
    public static function monthIDFilter($fieldName = 'monthID', $required = FALSE) {
        $factory = new InputFactory();
        return $factory->createInputFilter(array(
            $fieldName => array(
                'name' => $fieldName,
                'required' => $required,
                'validators' => array(
                    array('name' => 'not_empty'),
                    array('name' => 'string_length', 'options' => array(
                        'min'=>'6', 'max'=>'6'
                    )),
                    array('name' => 'regex', 'options'=>array(
                        'pattern' => '/^[0-9]{6}$/'
                    ))
                ),
            )
        ));
    }
}

Model:

class DataModel implements \Zend\InputFilter\InputFilterAwareInterface {
    protected $_inputFilter;

    public function __construct()
    {
        $this->setInputFilter(InputFilters::monthIDFilter());
    }

    public function setInputFilter(InputFilterInterface $inputfilter)
    {
        if(!$this->_inputFilter) {
            $this->_inputFilter = new InputFilter();
        }
        $this->_inputFilter->add($inputfilter);
    }

    public function getInputFilter()
    {
        if(!$this->_inputFilter) {
            $this->_inputFilter = new InputFilter();
        }
        return $this->_inputFilter;
    }
}

And finally, in the controller:

public function dataAction()
{
    $model = new DataModel();
    $inputFilter = $model->getInputFilter();
    $inputFilter->setData($_REQUEST);
    print_r($_REQUEST);
    print_r($inputFilter->getValues());
    if ($inputFilter->isValid()) {
        if ($inputFilter->has('monthID')) {
            echo $inputFilter->getValue('monthID');
            $formMonth = $inputFilter->getValue('monthID');
        } else {
            echo 'nope';
            $formMonth = '';
        }
        $grid->setSqlString($formMonth);
        $data = $grid->outputData();
        Json::out($data);
    }

In my output, I see the input that contains my monthID (201212) and an array that should be the validation results: Array ( [] => Array ( [monthID] => ) ) and the word 'nope' indicating that the has('monthID') check failed.

Any advice as to where I went wrong?

  • 写回答

1条回答 默认 最新

  • dongshuo9350 2013-01-16 15:40
    关注

    Try Changing createInputFilter to createInput inside your DataModel.

        return $factory->createInput(array(
            $fieldName => array(
                'name' => $fieldName,
                'required' => $required,
                'validators' => array(
                    array('name' => 'not_empty'),
                    array('name' => 'string_length', 'options' => array(
                        'min'=>'6', 'max'=>'6'
                    )),
                    array('name' => 'regex', 'options'=>array(
                        'pattern' => '/^[0-9]{6}$/'
                    ))
                ),
            )
        ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services