Permalink
Browse files

Add an option to create an empty option

  • Loading branch information...
1 parent 7e985d7 commit d651f559262bcb303b156d4e49d548402c3081bb @bakura10 bakura10 committed Sep 2, 2012
@@ -48,6 +48,14 @@ class MonthSelect extends Element implements InputProviderInterface, ElementPrep
protected $maxYear;
/**
+ * If set to true, it will generate an empty option for every select (this is mainly needed by most JavaScript
+ * libraries to allow to have a placeholder)
+ *
+ * @var bool
+ */
+ protected $createEmptyOption = false;
+
+ /**
* @var ValidatorInterface
*/
protected $validator;
@@ -61,13 +69,13 @@ class MonthSelect extends Element implements InputProviderInterface, ElementPrep
*/
public function __construct($name = null, $options = array())
{
- parent::__construct($name, $options);
+ $this->minYear = date('Y') - 100;
+ $this->maxYear = date('Y');
$this->monthElement = new Select('month');
$this->yearElement = new Select('year');
- $this->maxYear = date('Y');
- $this->minYear = $this->maxYear - 100;
+ parent::__construct($name, $options);
}
/**
@@ -90,6 +98,10 @@ public function setOptions($options)
$this->setMaxYear($options['max_year']);
}
+ if (isset($options['create_empty_option'])) {
+ $this->setShouldCreateEmptyOption($options['create_empty_option']);
+ }
+
return $this;
}
@@ -111,7 +123,7 @@ public function getYearElement()
/**
* @param int $minYear
- * @return DateSelect
+ * @return MonthSelect
*/
public function setMinYear($minYear)
{
@@ -129,7 +141,7 @@ public function getMinYear()
/**
* @param int $maxYear
- * @return DateSelect
+ * @return MonthSelect
*/
public function setMaxYear($maxYear)
{
@@ -146,6 +158,24 @@ public function getMaxYear()
}
/**
+ * @param bool $createEmptyOption
+ * @return MonthSelect
+ */
+ public function setShouldCreateEmptyOption($createEmptyOption)
+ {
+ $this->createEmptyOption = (bool) $createEmptyOption;
+ return $this;
+ }
+
+ /**
+ * @return bool
+ */
+ public function shouldCreateEmptyOption()
+ {
+ return $this->createEmptyOption;
+ }
+
+ /**
* @param mixed $value
* @return void|\Zend\Form\Element
*/
@@ -13,6 +13,7 @@
use DateTime;
use IntlDateFormatter;
use Zend\Form\ElementInterface;
+use Zend\Form\Element\DateSelect as DateSelectElement;
use Zend\Form\Exception;
use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
@@ -27,11 +28,19 @@ class FormDateSelect extends FormMonthSelectHelper
* Render a date element that is composed of three selects
*
* @param ElementInterface $element
- * @throws Exception\DomainException
+ * @throws \Zend\Form\Exception\InvalidArgumentException
+ * @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
+ if (!$element instanceof DateSelectElement) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ '%s requires that the element is of type Zend\Form\Element\Select',
+ __METHOD__
+ ));
+ }
+
$name = $element->getName();
if ($name === null || $name === '') {
throw new Exception\DomainException(sprintf(
@@ -47,9 +56,15 @@ public function render(ElementInterface $element)
$monthsOptions = $this->getMonthsOptions($pattern['month']);
$yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
- $dayElement = $element->getDayElement()->setAttribute('options', $daysOptions);
- $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
- $yearElement = $element->getYearElement()->setAttribute('options', $yearOptions);
+ if ($element->shouldCreateEmptyOption()) {
+ $daysOptions = array('' => '') + $daysOptions;
+ $monthsOptions = array('' => '') + $monthsOptions;
+ $yearOptions = array('' => '') + $yearOptions;
+ }
+
+ $dayElement = $element->getDayElement()->setValueOptions($daysOptions);
+ $monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
+ $yearElement = $element->getYearElement()->setValueOptions($yearOptions);
$markup = array();
$markup[$pattern['day']] = $selectHelper->render($dayElement);
@@ -74,7 +89,7 @@ public function render(ElementInterface $element)
* @param string $pattern Pattern to use for days
* @return array
*/
- public function getDaysOptions($pattern)
+ protected function getDaysOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'dd');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
@@ -14,6 +14,7 @@
use IntlDateFormatter;
use Locale;
use Zend\Form\ElementInterface;
+use Zend\Form\Element\MonthSelect as MonthSelectElement;
use Zend\Form\Exception;
/**
@@ -51,15 +52,24 @@ class FormMonthSelect extends AbstractHelper
*/
protected $locale;
+
/**
* Render a month element that is composed of two selects
*
* @param \Zend\Form\ElementInterface $element
+ * @throws \Zend\Form\Exception\InvalidArgumentException
* @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
+ if (!$element instanceof MonthSelectElement) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ '%s requires that the element is of type Zend\Form\Element\Select',
+ __METHOD__
+ ));
+ }
+
$name = $element->getName();
if ($name === null || $name === '') {
throw new Exception\DomainException(sprintf(
@@ -78,8 +88,13 @@ public function render(ElementInterface $element)
$monthsOptions = $this->getMonthsOptions($pattern['month']);
$yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
- $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
- $yearElement = $element->getYearElement()->setAttribute('options', $yearOptions);
+ if ($element->shouldCreateEmptyOption()) {
+ $monthsOptions = array('' => '') + $monthsOptions;
+ $yearOptions = array('' => '') + $yearOptions;
+ }
+
+ $monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
+ $yearElement = $element->getYearElement()->setValueOptions($yearOptions);
$markup = array();
$markup[$pattern['month']] = $selectHelper->render($monthElement);
@@ -211,7 +226,7 @@ public function getLocale()
* @param string $pattern Pattern to use for months
* @return array
*/
- public function getMonthsOptions($pattern)
+ protected function getMonthsOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'MM');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
@@ -238,7 +253,7 @@ public function getMonthsOptions($pattern)
* @param int $maxYear
* @return array
*/
- public function getYearsOptions($minYear, $maxYear)
+ protected function getYearsOptions($minYear, $maxYear)
{
$result = array();
for ($i = $maxYear; $i >= $minYear; --$i) {
@@ -42,6 +42,17 @@ public function testGeneratesThreeSelectsWithElement()
$this->assertContains('<select name="year"', $markup);
}
+ public function testCanGenerateSelectsWithEmptyOption()
+ {
+ $element = new DateSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $markup = $this->helper->render($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<option value=""></option>', $markup);
+ }
+
public function testInvokeProxiesToRender()
{
$element = new DateSelect('foo');
@@ -42,6 +42,17 @@ public function testGeneratesTwoSelectsWithElement()
$this->assertContains('<select name="year"', $markup);
}
+ public function testCanGenerateSelectsWithEmptyOption()
+ {
+ $element = new MonthSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $markup = $this->helper->render($element);
+ $this->assertNotContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<option value=""></option>', $markup);
+ }
+
public function testInvokeProxiesToRender()
{
$element = new MonthSelect('foo');

0 comments on commit d651f55

Please sign in to comment.