Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Illuminate/Html/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,61 @@ public function select($name, $list = array(), $selected = null, $options = arra
return "<select{$options}>{$list}</select>";
}

/**
* 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 = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP 5.3 please, Jeffrey :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah. :) Dang muscle memory.

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.
*
Expand Down
41 changes: 37 additions & 4 deletions tests/Html/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testFormTextarea()
public function testSelect()
{
$select = $this->formBuilder->select(
'size',
'size',
array('L' => 'Large', 'S' => 'Small')
);
$this->assertEquals($select, '<select name="size"><option value="L">Large</option><option value="S">Small</option></select>');
Expand All @@ -153,23 +153,56 @@ public function testSelect()

$select = $this->formBuilder->select(
'size',
array('L' => 'Large', 'S' => 'Small'),
array('L' => 'Large', 'S' => 'Small'),
'L'
);
$this->assertEquals($select, '<select name="size"><option value="L" selected="selected">Large</option><option value="S">Small</option></select>');



$select = $this->formBuilder->select(
'size',
array('L' => 'Large', 'S' => 'Small'),
'size',
array('L' => 'Large', 'S' => 'Small'),
null,
array('class' => 'class-name', 'id' => 'select-id')
);
$this->assertEquals($select, '<select class="class-name" id="select-id" name="size"><option value="L">Large</option><option value="S">Small</option></select>');
}


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('<select name="year"><option value="2000">2000</option><option value="2001">2001</option>', $select1);
$this->assertContains('<select id="foo" name="year"><option value="2000">2000</option><option value="2001">2001</option>', $select2);
$this->assertContains('<select name="year"><option value="2000" selected="selected">2000</option><option value="2001">2001</option>', $select3);
}


public function testFormSelectRange()
{
$range = $this->formBuilder->selectRange('dob', 1900, 2013);

$this->assertContains('<select name="dob"><option value="1900">1900</option>', $range);
$this->assertContains('<option value="2013">2013</option>', $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('<select name="month"><option value="1">January</option>', $month1);
$this->assertContains('<select name="month"><option value="1" selected="selected">January</option>', $month2);
$this->assertContains('<select id="foo" name="month"><option value="1">January</option>', $month3);
}


public function testFormCheckbox()
{
$form1 = $this->formBuilder->input('checkbox', 'foo');
Expand Down