Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

getValue() for MonthSelect, DateSelect and DateTimeSelect Form Elements #4756

Closed
wants to merge 7 commits into from

3 participants

@richardjennings

Tests added to check it is possible to get the value set on a DateTime,Date or Month Select Form Element.

getValue() methods implemented for each DateTime,Date and Month Select Form Elements using sprintf to return the designated format.

Fixes Issue #4755

@ThomasCantonnet

I reckon they should return DateTime objects, which would make more sense IMHO.

@richardjennings

@ThomasCantonnet
The return format is specified already as a filter callback in getInputSpecification()

For example $form->getData() will return 'year-month-day' for a DateSelect Element ( here )

The getValue() implementations added return a format identical to $form->getData();

@weierophinney weierophinney was assigned
@weierophinney

Merged to develop for release with 2.3.0, as it's a slight behavior change, and I want folks to test this a bit before we offer it in a release.

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
12 library/Zend/Form/Element/DateSelect.php
@@ -115,6 +115,18 @@ public function setValue($value)
}
/**
+ * @return String
+ */
+ public function getValue()
+ {
+ return sprintf('%s-%s-%s',
+ $this->getYearElement()->getValue(),
+ $this->getMonthElement()->getValue(),
+ $this->getDayElement()->getValue()
+ );
+ }
+
+ /**
* Prepare the form element (mostly used for rendering purposes)
*
* @param FormInterface $form
View
15 library/Zend/Form/Element/DateTimeSelect.php
@@ -244,6 +244,21 @@ public function setValue($value)
}
/**
+ * @return String
+ */
+ public function getValue()
+ {
+ return sprintf('%s-%s-%s %s:%s:%s',
+ $this->getYearElement()->getValue(),
+ $this->getMonthElement()->getValue(),
+ $this->getDayElement()->getValue(),
+ $this->getHourElement()->getValue(),
+ $this->getMinuteElement()->getValue(),
+ $this->getSecondElement()->getValue()
+ );
+ }
+
+ /**
* Prepare the form element (mostly used for rendering purposes)
*
* @param FormInterface $form
View
11 library/Zend/Form/Element/MonthSelect.php
@@ -277,6 +277,17 @@ public function setValue($value)
}
/**
+ * @return String
+ */
+ public function getValue()
+ {
+ return sprintf('%s-%s',
+ $this->getYearElement()->getValue(),
+ $this->getMonthElement()->getValue()
+ );
+ }
+
+ /**
* Prepare the form element (mostly used for rendering purposes)
*
* @param FormInterface $form
View
8 tests/ZendTest/Form/Element/DateSelectTest.php
@@ -62,6 +62,14 @@ public function testCanSetDateFromString()
$this->assertEquals('24', $element->getDayElement()->getValue());
}
+ public function testCanGetValue()
+ {
+ $element = new DateSelectElement();
+ $element->setValue(new DateTime('2012-09-24'));
+
+ $this->assertEquals('2012-09-24', $element->getValue());
+ }
+
/**
* @expectedException \Zend\Form\Exception\InvalidArgumentException
*/
View
8 tests/ZendTest/Form/Element/DateTimeSelectTest.php
@@ -88,6 +88,14 @@ public function testCanSetDateFromString()
$this->assertEquals('05', $element->getSecondElement()->getValue());
}
+ public function testCanGetValue()
+ {
+ $element = new DateTimeSelectElement();
+ $element->setValue(new DateTime('2012-09-24 03:04:05'));
+
+ $this->assertEquals('2012-09-24 03:04:05', $element->getValue());
+ }
+
/**
* @expectedException \Zend\Form\Exception\InvalidArgumentException
*/
View
7 tests/ZendTest/Form/Element/MonthSelectTest.php
@@ -81,4 +81,11 @@ public function testCanSetMonthFromDateTime()
$this->assertEquals('2012', $element->getYearElement()->getValue());
$this->assertEquals('09', $element->getMonthElement()->getValue());
}
+
+ public function testCanGetValue()
+ {
+ $element = new MonthSelectElement();
+ $element->setValue(new DateTime('2012-09'));
+ $this->assertEquals('2012-09', $element->getValue());
+ }
}
Something went wrong with that request. Please try again.