From 0bffe635b148368b01857e615ec802da7e1d7274 Mon Sep 17 00:00:00 2001 From: Jeffrey Way Date: Tue, 4 Jun 2013 15:07:17 -0400 Subject: [PATCH 1/3] Add selectMonth, selectRange, and selectYear methods. Closes #1502 --- src/Illuminate/Html/FormBuilder.php | 55 +++++++++++++++++++++++++++++ tests/Html/FormBuilderTest.php | 41 ++++++++++++++++++--- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Html/FormBuilder.php b/src/Illuminate/Html/FormBuilder.php index c8b37437ce41..0ac6461789b7 100755 --- a/src/Illuminate/Html/FormBuilder.php +++ b/src/Illuminate/Html/FormBuilder.php @@ -402,6 +402,61 @@ public function select($name, $list = array(), $selected = null, $options = arra return "{$list}"; } + /** + * Create a select range field. + * + * @param string $name + * @param string $begin + * @param string $end + * @param string $selected + * @param array $options + * @return string + */ + public function selectRange($name, $begin, $end, $selected = null, $options = array()) + { + $range = range($begin, $end); + + // We want the value for each option to + // be the same as the text content + $range = array_combine($range, $range); + + return $this->select($name, $range, $selected, $options); + } + + /** + * Create a select year field. + * + * @param string $name + * @param string $begin + * @param string $end + * @param string $selected + * @param array $options + * @return string + */ + public function selectYear() + { + return call_user_func_array(array($this, 'selectRange'), func_get_args()); + } + + /** + * Create a select month field. + * + * @param string $name + * @param string $selected + * @param array $options + * @return string + */ + public function selectMonth($name, $selected = null, $options = array()) + { + $months = []; + foreach (range(1, 12) as $month) + { + $months[$month] = date('F', mktime(0, 0, 0, $month)); + } + + return $this->select($name, $months, $selected, $options); + } + /** * Get the select option for the given value. * diff --git a/tests/Html/FormBuilderTest.php b/tests/Html/FormBuilderTest.php index cefca0b0dc27..42e77e22fa60 100755 --- a/tests/Html/FormBuilderTest.php +++ b/tests/Html/FormBuilderTest.php @@ -144,7 +144,7 @@ public function testFormTextarea() public function testSelect() { $select = $this->formBuilder->select( - 'size', + 'size', array('L' => 'Large', 'S' => 'Small') ); $this->assertEquals($select, ''); @@ -153,7 +153,7 @@ public function testSelect() $select = $this->formBuilder->select( 'size', - array('L' => 'Large', 'S' => 'Small'), + array('L' => 'Large', 'S' => 'Small'), 'L' ); $this->assertEquals($select, ''); @@ -161,8 +161,8 @@ public function testSelect() $select = $this->formBuilder->select( - 'size', - array('L' => 'Large', 'S' => 'Small'), + 'size', + array('L' => 'Large', 'S' => 'Small'), null, array('class' => 'class-name', 'id' => 'select-id') ); @@ -170,6 +170,39 @@ public function testSelect() } + public function testFormSelectYear() + { + $select1 = $this->formBuilder->selectYear('year', 2000, 2020); + $select2 = $this->formBuilder->selectYear('year', 2000, 2020, null, array('id' => 'foo')); + $select3 = $this->formBuilder->selectYear('year', 2000, 2020, '2000'); + + $this->assertContains('', $select2); + $this->assertContains('', $range); + $this->assertContains('', $range); + } + + + public function testFormSelectMonth() + { + $month1 = $this->formBuilder->selectMonth('month'); + $month2 = $this->formBuilder->selectMonth('month', '1'); + $month3 = $this->formBuilder->selectMonth('month', null, array('id' => 'foo')); + + $this->assertContains('', $month2); + $this->assertContains('