3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Form\Element
;
13 use DateTime
as PhpDateTime
;
14 use Zend\Form\Element
;
15 use Zend\InputFilter\InputProviderInterface
;
16 use Zend\Validator\Date
as DateValidator
;
17 use Zend\Validator\DateStep
as DateStepValidator
;
18 use Zend\Validator\GreaterThan
as GreaterThanValidator
;
19 use Zend\Validator\LessThan
as LessThanValidator
;
21 class DateTime
extends Element
implements InputProviderInterface
23 const DATETIME_FORMAT
= 'Y-m-d\TH:iP';
30 protected $attributes = array(
35 * A valid format string accepted by date()
39 protected $format = self
::DATETIME_FORMAT
;
44 protected $validators;
47 * Accepted options for DateTime:
48 * - format: A \DateTime compatible string
50 * @param array|\Traversable $options
53 public function setOptions($options)
55 parent
::setOptions($options);
57 if (isset($this->options
['format'])) {
58 $this->setFormat($this->options
['format']);
65 * Retrieve the element value
67 * If the value is a DateTime object, and $returnFormattedValue is true
68 * (the default), we return the string
69 * representation using the currently registered format.
71 * If $returnFormattedValue is false, the original value will be
72 * returned, regardless of type.
74 * @param bool $returnFormattedValue
77 public function getValue($returnFormattedValue = true)
79 $value = parent
::getValue();
80 if (!$value instanceof PhpDateTime ||
!$returnFormattedValue) {
83 $format = $this->getFormat();
84 return $value->format($format);
88 * Set value for format
90 * @param string $format
93 public function setFormat($format)
95 $this->format
= (string) $format;
100 * Retrieve the DateTime format to use for the value
104 public function getFormat()
106 return $this->format
;
114 protected function getValidators()
116 if ($this->validators
) {
117 return $this->validators
;
120 $validators = array();
121 $validators[] = $this->getDateValidator();
123 if (isset($this->attributes
['min'])) {
124 $validators[] = new GreaterThanValidator(array(
125 'min' => $this->attributes
['min'],
129 if (isset($this->attributes
['max'])) {
130 $validators[] = new LessThanValidator(array(
131 'max' => $this->attributes
['max'],
135 if (!isset($this->attributes
['step'])
136 ||
'any' !== $this->attributes
['step']
138 $validators[] = $this->getStepValidator();
141 $this->validators
= $validators;
142 return $this->validators
;
146 * Retrieves a Date Validator configured for a DateTime Input type
150 protected function getDateValidator()
152 return new DateValidator(array('format' => $this->format
));
156 * Retrieves a DateStep Validator configured for a DateTime Input type
160 protected function getStepValidator()
162 $format = $this->getFormat();
163 $stepValue = (isset($this->attributes
['step']))
164 ?
$this->attributes
['step'] : 1; // Minutes
166 $baseValue = (isset($this->attributes
['min']))
167 ?
$this->attributes
['min'] : date($format, 0);
169 return new DateStepValidator(array(
171 'baseValue' => $baseValue,
172 'step' => new DateInterval("PT{$stepValue}M"),
177 * Provide default input rules for this element
179 * Attaches default validators for the datetime input.
183 public function getInputSpecification()
186 'name' => $this->getName(),
189 array('name' => 'Zend\Filter\StringTrim'),
191 'validators' => $this->getValidators(),