This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateSelect and MonthSelect elements
- Loading branch information
Showing
11 changed files
with
872 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,236 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
* @package Zend_Form | |||
*/ | |||
|
|||
namespace Zend\Form\Element; | |||
|
|||
use DateTime; | |||
use Zend\Form\Element; | |||
use Zend\Form\ElementPrepareAwareInterface; | |||
use Zend\Form\Form; | |||
use Zend\InputFilter\InputProviderInterface; | |||
use Zend\Validator\ValidatorInterface; | |||
use Zend\Validator\Date as DateValidator; | |||
|
|||
class DateSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface | |||
{ | |||
/** | |||
* Select form element that contains values for day | |||
* | |||
* @var Select | |||
*/ | |||
protected $dayElement; | |||
|
|||
/** | |||
* Select form element that contains values for month | |||
* | |||
* @var Select | |||
*/ | |||
protected $monthElement; | |||
|
|||
/** | |||
* Select form element that contains values for year | |||
* | |||
* @var Select | |||
*/ | |||
protected $yearElement; | |||
|
|||
/** | |||
* @var ValidatorInterface | |||
*/ | |||
protected $validator; | |||
|
|||
/** | |||
* Min year to use for the select (default: current year - 100) | |||
* | |||
* @var int | |||
*/ | |||
protected $minYear; | |||
|
|||
/** | |||
* Max year to use for the select (default: current year) | |||
* | |||
* @var int | |||
*/ | |||
protected $maxYear; | |||
|
|||
/** | |||
* Constructor. Add three selects elements | |||
* | |||
* @param null|int|string $name Optional name for the element | |||
* @param array $options Optional options for the element | |||
*/ | |||
public function __construct($name = null, $options = array()) | |||
{ | |||
parent::__construct($name, $options); | |||
|
|||
$this->dayElement = new Select('day'); | |||
$this->monthElement = new Select('month'); | |||
$this->yearElement = new Select('year'); | |||
|
|||
$this->maxYear = date('Y'); | |||
$this->minYear = $this->maxYear - 100; | |||
} | |||
|
|||
/** | |||
* Accepted options for DateSelect: | |||
* - min_year: min year to use in the year select | |||
* - max_year: max year to use in the year select | |||
* | |||
* @param array|\Traversable $options | |||
* @return DateSelect | |||
*/ | |||
public function setOptions($options) | |||
{ | |||
parent::setOptions($options); | |||
|
|||
if (isset($options['min_year'])) { | |||
$this->setMinYear($options['min_year']); | |||
} | |||
|
|||
if (isset($options['max_year'])) { | |||
$this->setMaxYear($options['max_year']); | |||
} | |||
|
|||
return $this; | |||
} | |||
|
|||
/** | |||
* @return Select | |||
*/ | |||
public function getDayElement() | |||
{ | |||
return $this->dayElement; | |||
} | |||
|
|||
/** | |||
* @return Select | |||
*/ | |||
public function getMonthElement() | |||
{ | |||
return $this->monthElement; | |||
} | |||
|
|||
/** | |||
* @return Select | |||
*/ | |||
public function getYearElement() | |||
{ | |||
return $this->yearElement; | |||
} | |||
|
|||
/** | |||
* @param int $minYear | |||
* @return DateSelect | |||
*/ | |||
public function setMinYear($minYear) | |||
{ | |||
$this->minYear = $minYear; | |||
return $this; | |||
} | |||
|
|||
/** | |||
* @return int | |||
*/ | |||
public function getMinYear() | |||
{ | |||
return $this->minYear; | |||
} | |||
|
|||
/** | |||
* @param int $maxYear | |||
* @return DateSelect | |||
*/ | |||
public function setMaxYear($maxYear) | |||
{ | |||
$this->maxYear = $maxYear; | |||
return $this; | |||
} | |||
|
|||
/** | |||
* @return int | |||
*/ | |||
public function getMaxYear() | |||
{ | |||
return $this->maxYear; | |||
} | |||
|
|||
/** | |||
* @param mixed $value | |||
* @return void|\Zend\Form\Element | |||
*/ | |||
public function setValue($value) | |||
{ | |||
$this->dayElement->setValue($value['day']); | |||
$this->monthElement->setValue($value['month']); | |||
$this->yearElement->setValue($value['year']); | |||
} | |||
|
|||
/** | |||
* Prepare the form element (mostly used for rendering purposes) | |||
* | |||
* @param Form $form | |||
* @return mixed | |||
*/ | |||
public function prepareElement(Form $form) | |||
{ | |||
$name = $this->getName(); | |||
|
|||
$this->dayElement->setName($name . '[day]'); | |||
$this->monthElement->setName($name . '[month]'); | |||
$this->yearElement->setName($name . '[year]'); | |||
} | |||
|
|||
/** | |||
* Get validator | |||
* | |||
* @return ValidatorInterface | |||
*/ | |||
protected function getValidator() | |||
{ | |||
if (null === $this->validator) { | |||
$this->validator = new DateValidator(array('format' => 'Y-n-j')); | |||
} | |||
|
|||
return $this->validator; | |||
} | |||
|
|||
/** | |||
* Should return an array specification compatible with | |||
* {@link Zend\InputFilter\Factory::createInput()}. | |||
* | |||
* @return array | |||
*/ | |||
public function getInputSpecification() | |||
{ | |||
return array( | |||
'name' => $this->getName(), | |||
'required' => false, | |||
'filters' => array( | |||
array( | |||
'name' => 'Callback', | |||
'options' => array( | |||
'callback' => function($date) { | |||
// Convert the date to a specific format | |||
if (is_array($date)) { | |||
$date = $date['year'] . '-' . $date['month'] . '-' . $date['day']; | |||
} | |||
|
|||
return $date; | |||
} | |||
) | |||
) | |||
), | |||
'validators' => array( | |||
$this->getValidator(), | |||
) | |||
); | |||
} | |||
} | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,70 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
* @package Zend_Form | |||
*/ | |||
|
|||
namespace Zend\Form\Element; | |||
|
|||
use Zend\Validator\ValidatorInterface; | |||
use Zend\Validator\Regex as RegexValidator; | |||
|
|||
class MonthSelect extends DateSelect | |||
{ | |||
/** | |||
* @param mixed $value | |||
* @return void|\Zend\Form\Element | |||
*/ | |||
public function setValue($value) | |||
{ | |||
$this->monthElement->setValue($value['month']); | |||
$this->yearElement->setValue($value['year']); | |||
} | |||
|
|||
/** | |||
* Get validator | |||
* | |||
* @return ValidatorInterface | |||
*/ | |||
protected function getValidator() | |||
{ | |||
return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/'); | |||
} | |||
|
|||
/** | |||
* Should return an array specification compatible with | |||
* {@link Zend\InputFilter\Factory::createInput()}. | |||
* | |||
* @return array | |||
*/ | |||
public function getInputSpecification() | |||
{ | |||
return array( | |||
'name' => $this->getName(), | |||
'required' => false, | |||
'filters' => array( | |||
array( | |||
'name' => 'Callback', | |||
'options' => array( | |||
'callback' => function($date) { | |||
// Convert the date to a specific format | |||
if (is_array($date)) { | |||
$date = $date['year'] . '-' . $date['month']; | |||
} | |||
|
|||
return $date; | |||
} | |||
) | |||
) | |||
), | |||
'validators' => array( | |||
$this->getValidator(), | |||
) | |||
); | |||
} | |||
} | |||
|
Oops, something went wrong.