0

In my app library I have Input filter which uses Zend_Form (ZF 1.*)

class My_InputFilter extends Zend_Form
{
    public function init()
    {
        $this->addElement(
            'text', 'dateFrom', array(
                'required' => true,
                'filters' => array(
                    'dateTimeTotimeStamp'
                ),
                'validators' => array(
                    array('date', false, array('format' => 'Y-m-d H:i:s'))
                )
            )
        );
    }
}

Then in my application I would like to get validated data in timestamp format:

$inputFilter = new My_InputFilter();
if (!$inputFilter->isValid($criteria)) {
    throw new InvalidArgumentException(
        $this->simplifyInputFilterErrorMessages($inputFilter->getMessages())
    );
} else {
    $validatedCriteria = $inputFilter->getValues(); // Array with my timestamp date
}

Where to implement 'dateTimeTotimeStamp' (example name) strtotime() filter to get validated and filtered date in UNIX timestamp format?

4

1 回答 1

3

如果您不想创建单独的过滤器,请使用Zend_Filter_Callback

例子:

$this->addElement($this
    ->createElement('text', 'dateFrom')
    ->addFilter(new Zend_Filter_Callback(function($value){
        return strtotime($value);
    })));

在验证期间和通过 getValue() 检索元素值时都会发生过滤:

于 2013-06-12T23:57:16.717 回答