Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

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

Closed
wants to merge 7 commits into from
12 changes: 12 additions & 0 deletions library/Zend/Form/Element/DateSelect.php
Expand Up @@ -114,6 +114,18 @@ public function setValue($value)
$this->dayElement->setValue($value['day']);
}

/**
* @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)
*
Expand Down
15 changes: 15 additions & 0 deletions library/Zend/Form/Element/DateTimeSelect.php
Expand Up @@ -243,6 +243,21 @@ public function setValue($value)
$this->secondElement->setValue($value['second']);
}

/**
* @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)
*
Expand Down
11 changes: 11 additions & 0 deletions library/Zend/Form/Element/MonthSelect.php
Expand Up @@ -276,6 +276,17 @@ public function setValue($value)
$this->monthElement->setValue($value['month']);
}

/**
* @return String
*/
public function getValue()
{
return sprintf('%s-%s',
$this->getYearElement()->getValue(),
$this->getMonthElement()->getValue()
);
}

/**
* Prepare the form element (mostly used for rendering purposes)
*
Expand Down
8 changes: 8 additions & 0 deletions tests/ZendTest/Form/Element/DateSelectTest.php
Expand Up @@ -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
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/ZendTest/Form/Element/DateTimeSelectTest.php
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/ZendTest/Form/Element/MonthSelectTest.php
Expand Up @@ -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());
}
}