Reputation: 772
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?
Upvotes: 0
Views: 436
Reputation: 8840
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():
Upvotes: 3