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
;
12 use DateTime
as PhpDateTime
;
13 use Zend\Form\Element
;
14 use Zend\Form\ElementPrepareAwareInterface
;
15 use Zend\Form\FormInterface
;
16 use Zend\InputFilter\InputProviderInterface
;
17 use Zend\Validator\Regex
as RegexValidator
;
18 use Zend\Validator\ValidatorInterface
;
20 class MonthSelect
extends Element
implements InputProviderInterface
, ElementPrepareAwareInterface
23 * Select form element that contains values for month
27 protected $monthElement;
30 * Select form element that contains values for year
34 protected $yearElement;
37 * Min year to use for the select (default: current year - 100)
44 * Max year to use for the select (default: current year)
51 * If set to true, it will generate an empty option for every select (this is mainly needed by most JavaScript
52 * libraries to allow to have a placeholder)
56 protected $createEmptyOption = false;
59 * If set to true, view helpers will render delimiters between <select> elements, according to the
64 protected $renderDelimiters = true;
67 * @var ValidatorInterface
73 * Constructor. Add two selects elements
75 * @param null|int|string $name Optional name for the element
76 * @param array $options Optional options for the element
78 public function __construct($name = null, $options = array())
80 $this->minYear
= date('Y') - 100;
81 $this->maxYear
= date('Y');
83 $this->monthElement
= new Select('month');
84 $this->yearElement
= new Select('year');
86 parent
::__construct($name, $options);
90 * Accepted options for DateSelect:
91 * - month_attributes: HTML attributes to be rendered with the month element
92 * - year_attributes: HTML attributes to be rendered with the month element
93 * - min_year: min year to use in the year select
94 * - max_year: max year to use in the year select
96 * @param array|\Traversable $options
99 public function setOptions($options)
101 parent
::setOptions($options);
103 if (isset($options['month_attributes'])) {
104 $this->setMonthAttributes($options['month_attributes']);
107 if (isset($options['year_attributes'])) {
108 $this->setYearAttributes($options['year_attributes']);
111 if (isset($options['min_year'])) {
112 $this->setMinYear($options['min_year']);
115 if (isset($options['max_year'])) {
116 $this->setMaxYear($options['max_year']);
119 if (isset($options['create_empty_option'])) {
120 $this->setShouldCreateEmptyOption($options['create_empty_option']);
123 if (isset($options['render_delimiters'])) {
124 $this->setShouldRenderDelimiters($options['render_delimiters']);
133 public function getMonthElement()
135 return $this->monthElement
;
141 public function getYearElement()
143 return $this->yearElement
;
147 * Set the month attributes
149 * @param array $monthAttributes
150 * @return MonthSelect
152 public function setMonthAttributes(array $monthAttributes)
154 $this->monthElement
->setAttributes($monthAttributes);
159 * Get the month attributes
163 public function getMonthAttributes()
165 return $this->monthElement
->getAttributes();
169 * Set the year attributes
171 * @param array $yearAttributes
172 * @return MonthSelect
174 public function setYearAttributes(array $yearAttributes)
176 $this->yearElement
->setAttributes($yearAttributes);
181 * Get the year attributes
185 public function getYearAttributes()
187 return $this->yearElement
->getAttributes();
191 * @param int $minYear
192 * @return MonthSelect
194 public function setMinYear($minYear)
196 $this->minYear
= $minYear;
203 public function getMinYear()
205 return $this->minYear
;
209 * @param int $maxYear
210 * @return MonthSelect
212 public function setMaxYear($maxYear)
214 $this->maxYear
= $maxYear;
221 public function getMaxYear()
223 return $this->maxYear
;
227 * @param bool $createEmptyOption
228 * @return MonthSelect
230 public function setShouldCreateEmptyOption($createEmptyOption)
232 $this->createEmptyOption
= (bool) $createEmptyOption;
239 public function shouldCreateEmptyOption()
241 return $this->createEmptyOption
;
245 * @param bool $renderDelimiters
246 * @return MonthSelect
248 public function setShouldRenderDelimiters($renderDelimiters)
250 $this->renderDelimiters
= (bool) $renderDelimiters;
257 public function shouldRenderDelimiters()
259 return $this->renderDelimiters
;
263 * @param mixed $value
264 * @return void|\Zend\Form\Element
266 public function setValue($value)
268 if ($value instanceof PhpDateTime
) {
270 'year' => $value->format('Y'),
271 'month' => $value->format('m')
275 $this->yearElement
->setValue($value['year']);
276 $this->monthElement
->setValue($value['month']);
280 * Prepare the form element (mostly used for rendering purposes)
282 * @param FormInterface $form
285 public function prepareElement(FormInterface
$form)
287 $name = $this->getName();
288 $this->monthElement
->setName($name . '[month]');
289 $this->yearElement
->setName($name . '[year]');
295 * @return ValidatorInterface
297 protected function getValidator()
299 return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/');
303 * Should return an array specification compatible with
304 * {@link Zend\InputFilter\Factory::createInput()}.
308 public function getInputSpecification()
311 'name' => $this->getName(),
315 'name' => 'Callback',
317 'callback' => function ($date) {
318 // Convert the date to a specific format
319 if (is_array($date)) {
320 $date = $date['year'] . '-' . $date['month'];
328 'validators' => array(
329 $this->getValidator(),
335 * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
337 public function __clone()
339 $this->monthElement
= clone $this->monthElement
;
340 $this->yearElement
= clone $this->yearElement
;