Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow to remove delimiters for DateSelect and fix bugs with some locales #3710

Closed
wants to merge 3 commits into from

4 participants

@bakura10

This is a fix for #3694

ping @wryck7 and @grizzm0, could you tell me if it solves your issue ?

@weierophinney weierophinney was assigned
@weierophinney

Merged to develop, as it introduces new methods and thus new behaviors.

@bakura10

I think it should be merged into master instead. It mainly fix a bug that would make both DateTimeSelect/DateSelect/MonthSelect not working with some locales like es_ES, pt_BR...

@weierophinney

Done.

@iquabius

In my case it's working - because I disabled 'render_delimiters'. But if someone needs the delimiters with es_ES or pt_BR, the delimiters will be wrong. See the exemples:

The pattern for es_ES and pt_BR is "d 'de' MMMM 'de' y", but when rendering, the helper outputs:

    <select name="day">...</select> '' <select name="month"> '' <select name="year">

note: The delimiters should be de (without single quotes arround) insted of ''

This is happening because the regex in the parsePattern() method, when $pattern = "d 'de' MMMM 'de' y", the $pregResult will be something like this:

array(9) {
  [0]=>
  string(1) "d"
  [1]=>
  string(2) " '"
  [2]=>
  string(2) "de"
  [3]=>
  string(2) "' "
  [4]=>
  string(4) "MMMM"
  [5]=>
  string(2) " '"
  [6]=>
  string(2) "de"
  [7]=>
  string(2) "' "
  [8]=>
  string(1) "y"
}
@iquabius

I just noticed another problem, when using formDateSelect() with locale pt_BR or es_ES, the helper generate a weird array of options for the days, this is what I get from the method getDaysOptions():

array(31) {
  ["01"]=>
  string(2) "15"
  ["02"]=>
  string(2) "26"
  ["03"]=>
  string(2) "37"
  ["04"]=>
  string(2) "41"
  ["05"]=>
  string(2) "52"
  ["06"]=>
  string(2) "63"
  ["07"]=>
  string(2) "74"
  ["08"]=>
  string(2) "85"
  ["09"]=>
  string(2) "96"
  [10]=>
  string(3) "107"
  [11]=>
  string(3) "111"
  [12]=>
  string(3) "122"
  [13]=>
  string(3) "133"
  [14]=>
  string(3) "144"
  [15]=>
  string(3) "155"
  [16]=>
  string(3) "166"
  [17]=>
  string(3) "177"
  [18]=>
  string(3) "181"
  [19]=>
  string(3) "192"
  [20]=>
  string(3) "203"
  [21]=>
  string(3) "214"
  [22]=>
  string(3) "225"
  [23]=>
  string(3) "236"
  [24]=>
  string(3) "247"
  [25]=>
  string(3) "251"
  [26]=>
  string(3) "262"
  [27]=>
  string(3) "273"
  [28]=>
  string(3) "284"
  [29]=>
  string(3) "295"
  [30]=>
  string(3) "306"
  [31]=>
  string(3) "317"
}

And this is happening because of the parsePattern() as well.

@bakura10

It looks like the keys are ok... This is driving me crazy :'(. I'll try to have a look tomorrow (or please check it, it must be a stupid error...).

@grizzm0

Do we really need Intl to render most of this stuff? I think it's a bit overkill. The only Intl use for days, hours, minutes and seconds would be to see if 0-9 should be double digit (not sure how that's handled in different locales).

@bakura10

That's an idea too. I think it'll be easier. I initially did that because I asked myself if some locale used "01" or "1".

@iquabius

I sent a PR here, take a look!

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/3710' into develop
Close #3710
Fixes #3694
130c2eb
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3710'
Close #3710
cec0d3b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
30 library/Zend/Form/Element/MonthSelect.php
@@ -56,6 +56,14 @@ class MonthSelect extends Element implements InputProviderInterface, ElementPrep
protected $createEmptyOption = false;
/**
+ * If set to true, view helpers will render delimiters between <select> elements, according to the
+ * specified locale
+ *
+ * @var bool
+ */
+ protected $renderDelimiters = true;
+
+ /**
* @var ValidatorInterface
*/
protected $validator;
@@ -112,6 +120,10 @@ public function setOptions($options)
$this->setShouldCreateEmptyOption($options['create_empty_option']);
}
+ if (isset($options['render_delimiters'])) {
+ $this->setShouldRenderDelimiters($options['render_delimiters']);
+ }
+
return $this;
}
@@ -230,6 +242,24 @@ public function shouldCreateEmptyOption()
}
/**
+ * @param bool $renderDelimiters
+ * @return MonthSelect
+ */
+ public function setShouldRenderDelimiters($renderDelimiters)
+ {
+ $this->renderDelimiters = (bool) $renderDelimiters;
+ return $this;
+ }
+
+ /**
+ * @return bool
+ */
+ public function shouldRenderDelimiters()
+ {
+ return $this->renderDelimiters;
+ }
+
+ /**
* @param mixed $value
* @return void|\Zend\Form\Element
*/
View
27 library/Zend/Form/View/Helper/FormDateSelect.php
@@ -44,7 +44,7 @@ public function render(ElementInterface $element)
}
$selectHelper = $this->getSelectElementHelper();
- $pattern = $this->parsePattern();
+ $pattern = $this->parsePattern($element->shouldRenderDelimiters());
$daysOptions = $this->getDaysOptions($pattern['day']);
$monthsOptions = $this->getMonthsOptions($pattern['month']);
@@ -60,19 +60,20 @@ public function render(ElementInterface $element)
$monthElement->setEmptyOption('');
}
- $markup = array();
- $markup[$pattern['day']] = $selectHelper->render($dayElement);
- $markup[$pattern['month']] = $selectHelper->render($monthElement);
- $markup[$pattern['year']] = $selectHelper->render($yearElement);
+ $data = array();
+ $data[$pattern['day']] = $selectHelper->render($dayElement);
+ $data[$pattern['month']] = $selectHelper->render($monthElement);
+ $data[$pattern['year']] = $selectHelper->render($yearElement);
- $markup = sprintf(
- '%s %s %s %s %s',
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)]
- );
+ $markup = '';
+ foreach ($pattern as $key => $value) {
+ // Delimiter
+ if (is_numeric($key)) {
+ $markup .= $value;
+ } else {
+ $markup .= $data[$value];
+ }
+ }
return $markup;
}
View
59 library/Zend/Form/View/Helper/FormDateTimeSelect.php
@@ -52,7 +52,7 @@ public function render(ElementInterface $element)
}
$selectHelper = $this->getSelectElementHelper();
- $pattern = $this->parsePattern();
+ $pattern = $this->parsePattern($element->shouldRenderDelimiters());
$daysOptions = $this->getDaysOptions($pattern['day']);
$monthsOptions = $this->getMonthsOptions($pattern['month']);
@@ -77,43 +77,27 @@ public function render(ElementInterface $element)
$secondElement->setEmptyOption('');
}
- $markup = array();
- $markup[$pattern['day']] = $selectHelper->render($dayElement);
- $markup[$pattern['month']] = $selectHelper->render($monthElement);
- $markup[$pattern['year']] = $selectHelper->render($yearElement);
- $markup[$pattern['hour']] = $selectHelper->render($hourElement);
- $markup[$pattern['minute']] = $selectHelper->render($minuteElement);
+ $data = array();
+ $data[$pattern['day']] = $selectHelper->render($dayElement);
+ $data[$pattern['month']] = $selectHelper->render($monthElement);
+ $data[$pattern['year']] = $selectHelper->render($yearElement);
+ $data[$pattern['hour']] = $selectHelper->render($hourElement);
+ $data[$pattern['minute']] = $selectHelper->render($minuteElement);
if ($element->shouldShowSeconds()) {
- $markup[$pattern['second']] = $selectHelper->render($secondElement);
-
- $markup = sprintf(
- '%s %s %s %s %s %s %s %s %s %s %s',
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)]
- );
+ $data[$pattern['second']] = $selectHelper->render($secondElement);
} else {
- $markup = sprintf(
- '%s %s %s %s %s %s %s %s %s',
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)]
- );
+ unset($pattern['second']);
+ }
+
+ $markup = '';
+ foreach ($pattern as $key => $value) {
+ // Delimiter
+ if (is_numeric($key)) {
+ $markup .= $value;
+ } else {
+ $markup .= $data[$value];
+ }
}
return $markup;
@@ -192,9 +176,10 @@ public function getPattern()
/**
* Parse the pattern
*
+ * @param bool $renderDelimiters
* @return array
*/
- protected function parsePattern()
+ protected function parsePattern($renderDelimiters = true)
{
$pattern = $this->getPattern();
$pregResult = preg_split('/([ -,.:\/]+)/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
@@ -213,7 +198,7 @@ protected function parsePattern()
$result['minute'] = $value;
} elseif (strpos($value, 's') !== false) {
$result['second'] = $value;
- } else {
+ } elseif ($renderDelimiters) {
$result[] = $value;
}
}
View
28 library/Zend/Form/View/Helper/FormMonthSelect.php
@@ -73,7 +73,7 @@ public function render(ElementInterface $element)
}
$selectHelper = $this->getSelectElementHelper();
- $pattern = $this->parsePattern();
+ $pattern = $this->parsePattern($element->shouldRenderDelimiters());
// The pattern always contains "day" part and the first separator, so we have to remove it
unset($pattern['day']);
@@ -90,16 +90,19 @@ public function render(ElementInterface $element)
$yearElement->setEmptyOption('');
}
- $markup = array();
- $markup[$pattern['month']] = $selectHelper->render($monthElement);
- $markup[$pattern['year']] = $selectHelper->render($yearElement);
+ $data = array();
+ $data[$pattern['month']] = $selectHelper->render($monthElement);
+ $data[$pattern['year']] = $selectHelper->render($yearElement);
- $markup = sprintf(
- '%s %s %s',
- $markup[array_shift($pattern)],
- array_shift($pattern), // Delimiter
- $markup[array_shift($pattern)]
- );
+ $markup = '';
+ foreach ($pattern as $key => $value) {
+ // Delimiter
+ if (is_numeric($key)) {
+ $markup .= $value;
+ } else {
+ $markup .= $data[$value];
+ }
+ }
return $markup;
}
@@ -145,9 +148,10 @@ public function getPattern()
/**
* Parse the pattern
*
+ * @param bool $renderDelimiters
* @return array
*/
- protected function parsePattern()
+ protected function parsePattern($renderDelimiters = true)
{
$pattern = $this->getPattern();
$pregResult = preg_split('/([ -,.\/]+)/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
@@ -160,7 +164,7 @@ protected function parsePattern()
$result['month'] = $value;
} elseif (stripos($value, 'y') !== false) {
$result['year'] = $value;
- } else {
+ } elseif ($renderDelimiters) {
$result[] = $value;
}
}
View
12 tests/ZendTest/Form/View/Helper/FormDateSelectTest.php
@@ -53,6 +53,18 @@ public function testCanGenerateSelectsWithEmptyOption()
$this->assertContains('<option value=""></option>', $markup);
}
+ public function testCanDisableDelimiters()
+ {
+ $element = new DateSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $element->setShouldRenderDelimiters(false);
+ $markup = $this->helper->render($element);
+
+ // If it contains wo consecutive selects this means that no delimiters
+ // are inserted
+ $this->assertContains('</select><select', $markup);
+ }
+
public function testInvokeProxiesToRender()
{
$element = new DateSelect('foo');
View
12 tests/ZendTest/Form/View/Helper/FormDateTimeSelectTest.php
@@ -71,6 +71,18 @@ public function testCanGenerateSelectsWithEmptyOption()
$this->assertContains('<option value=""></option>', $markup);
}
+ public function testCanDisableDelimiters()
+ {
+ $element = new DateTimeSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $element->setShouldRenderDelimiters(false);
+ $markup = $this->helper->render($element);
+
+ // If it contains wo consecutive selects this means that no delimiters
+ // are inserted
+ $this->assertContains('</select><select', $markup);
+ }
+
public function testInvokeProxiesToRender()
{
$element = new DateTimeSelect('foo');
View
12 tests/ZendTest/Form/View/Helper/FormMonthSelectTest.php
@@ -53,6 +53,18 @@ public function testCanGenerateSelectsWithEmptyOption()
$this->assertContains('<option value=""></option>', $markup);
}
+ public function testCanDisableDelimiters()
+ {
+ $element = new MonthSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $element->setShouldRenderDelimiters(false);
+ $markup = $this->helper->render($element);
+
+ // If it contains wo consecutive selects this means that no delimiters
+ // are inserted
+ $this->assertContains('</select><select', $markup);
+ }
+
public function testInvokeProxiesToRender()
{
$element = new MonthSelect('foo');
Something went wrong with that request. Please try again.