3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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
;
12 use DateTime
as PhpDateTime
;
13 use Zend\Form\Exception\InvalidArgumentException
;
14 use Zend\Form\FormInterface
;
15 use Zend\Validator\ValidatorInterface
;
16 use Zend\Validator\Date
as DateValidator
;
19 class DateSelect
extends MonthSelect
22 * Select form element that contains values for day
26 protected $dayElement;
29 * Constructor. Add the day select element
31 * @param null|int|string $name Optional name for the element
32 * @param array $options Optional options for the element
34 public function __construct($name = null, $options = array())
36 $this->dayElement
= new Select('day');
38 parent
::__construct($name, $options);
42 * Accepted options for DateSelect (plus the ones from MonthSelect) :
43 * - day_attributes: HTML attributes to be rendered with the day element
45 * @param array|\Traversable $options
48 public function setOptions($options)
50 parent
::setOptions($options);
52 if (isset($options['day_attributes'])) {
53 $this->setDayAttributes($options['day_attributes']);
62 public function getDayElement()
64 return $this->dayElement
;
68 * Get both the year and month elements
72 public function getElements()
74 return array_merge(array($this->dayElement
), parent
::getElements());
78 * Set the day attributes
80 * @param array $dayAttributes
83 public function setDayAttributes(array $dayAttributes)
85 $this->dayElement
->setAttributes($dayAttributes);
90 * Get the day attributes
94 public function getDayAttributes()
96 return $this->dayElement
->getAttributes();
100 * @param string|array|\ArrayAccess|PhpDateTime $value
101 * @throws \Zend\Form\Exception\InvalidArgumentException
102 * @return self Provides a fluent interface
104 public function setValue($value)
106 if (is_string($value)) {
108 $value = new PhpDateTime($value);
109 } catch (Exception
$e) {
110 throw new InvalidArgumentException('Value should be a parsable string or an instance of DateTime');
114 if ($value instanceof PhpDateTime
) {
116 'year' => $value->format('Y'),
117 'month' => $value->format('m'),
118 'day' => $value->format('d'),
122 $this->yearElement
->setValue($value['year']);
123 $this->monthElement
->setValue($value['month']);
124 $this->dayElement
->setValue($value['day']);
132 public function getValue()
136 $this->getYearElement()->getValue(),
137 $this->getMonthElement()->getValue(),
138 $this->getDayElement()->getValue()
143 * Prepare the form element (mostly used for rendering purposes)
145 * @param FormInterface $form
148 public function prepareElement(FormInterface
$form)
150 parent
::prepareElement($form);
152 $name = $this->getName();
153 $this->dayElement
->setName($name . '[day]');
159 * @return ValidatorInterface
161 protected function getValidator()
163 if (null === $this->validator
) {
164 $this->validator
= new DateValidator(array('format' => 'Y-m-d'));
167 return $this->validator
;
171 * Should return an array specification compatible with
172 * {@link Zend\InputFilter\Factory::createInput()}.
176 public function getInputSpecification()
179 'name' => $this->getName(),
182 array('name' => 'DateSelect')
184 'validators' => array(
185 $this->getValidator(),
191 * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
193 public function __clone()
195 $this->dayElement
= clone $this->dayElement
;
196 $this->monthElement
= clone $this->monthElement
;
197 $this->yearElement
= clone $this->yearElement
;