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?

1 Answer 1

3

If you don't want to create separate filter use Zend_Filter_Callback

Example:

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

Filtering happens both during validation and when you retrieve the element value via getValue():

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.