Skip to content

Commit

Permalink
add default value parameter to checkboxGroup and radioGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Aug 11, 2016
1 parent f76c339 commit 772ee10
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/SemanticForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,23 @@ public function checkbox($name, $value = 1)
return $checkbox;
}

public function checkboxGroup($name, $options)
public function checkboxGroup($name, $options, $checked = [])
{
$checked = (array)$checked;
$controls = [];
$oldValue = $this->getValueFor($name);

foreach($options as $value => $label) {
$radio = (new Checkbox($name . "[$value]", $value))->label($label);
if ($value == $oldValue) {
$radio->check();

if($oldValue !== null) {
if (in_array($value, $oldValue)) {
$radio->check();
}
} else {
if (in_array($value, $checked)) {
$radio->check();
}
}

$controls[] = $radio;
Expand All @@ -203,17 +211,17 @@ public function radio($name, $value = null)
return $radio;
}

public function radioGroup($name, $options)
public function radioGroup($name, $options, $checked = null)
{
$controls = [];
$oldValue = $this->getValueFor($name);

foreach($options as $value => $label) {
$radio = (new RadioButton($name, $value))->label($label);
if ($value == $oldValue) {

if (($oldValue !== null && $value == $oldValue) || ($oldValue === null && $value == $checked)) {
$radio->check();
}

$controls[] = $radio;
}

Expand Down
111 changes: 111 additions & 0 deletions tests/SemanticFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,59 @@ public function testRadioGroup()
$this->assertEquals($expected, $result);
}

public function testRadioGroupWithValue()
{
$expected = '<div class="grouped fields">';
$expected .= '<label>Fruit</label>';
$expected .= '<div class="field">';
$expected .= '<div class="ui radio checkbox">';
$expected .= '<input type="radio" name="fruit" value="orange">';
$expected .= '<label>Orange</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '<div class="field">';
$expected .= '<div class="ui radio checkbox">';
$expected .= '<input type="radio" name="fruit" value="banana" checked="checked">';
$expected .= '<label>Banana</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '</div>';

$options = ['orange' => 'Orange', 'banana'=>'Banana'];
$result = (string)$this->form->radioGroup('fruit', $options, 'banana')->label('Fruit');
$this->assertEquals($expected, $result);
}

public function testRadioGroupWithValueAndOldInput()
{
$oldInput = Mockery::mock('Laravolt\SemanticForm\OldInput\OldInputInterface');
$oldInput->shouldReceive('hasOldInput')->andReturn(true);
$oldInput->shouldReceive('getOldInput')->with('fruit')->andReturn('orange');

$this->form->setOldInputProvider($oldInput);


$expected = '<div class="grouped fields">';
$expected .= '<label>Fruit</label>';
$expected .= '<div class="field">';
$expected .= '<div class="ui radio checkbox">';
$expected .= '<input type="radio" name="fruit" value="orange" checked="checked">';
$expected .= '<label>Orange</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '<div class="field">';
$expected .= '<div class="ui radio checkbox">';
$expected .= '<input type="radio" name="fruit" value="banana">';
$expected .= '<label>Banana</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '</div>';

$options = ['orange' => 'Orange', 'banana'=>'Banana'];
$result = (string)$this->form->radioGroup('fruit', $options, 'banana')->label('Fruit');
$this->assertEquals($expected, $result);
}

public function testRadioGroupInline()
{
$expected = '<div class="inline fields">';
Expand Down Expand Up @@ -177,6 +230,64 @@ public function testCheckboxGroup()
$this->assertEquals($expected, $result);
}

public function testCheckboxGroupWithValue()
{
$expected = '<div class="grouped fields">';
$expected .= '<label>Fruit</label>';
$expected .= '<div class="field">';
$expected .= '<div class="ui checkbox">';
$expected .= '<input type="checkbox" name="fruit[orange]" value="orange">';
$expected .= '<label>Orange</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '<div class="field">';
$expected .= '<div class="ui checkbox">';
$expected .= '<input type="checkbox" name="fruit[banana]" value="banana" checked="checked">';
$expected .= '<label>Banana</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '</div>';

$options = ['orange' => 'Orange', 'banana'=>'Banana'];
$result = (string)$this->form->checkboxGroup('fruit', $options, ['banana'])->label('Fruit');
$this->assertEquals($expected, $result);

$result = (string)$this->form->checkboxGroup('fruit', $options, 'banana')->label('Fruit');
$this->assertEquals($expected, $result);
}

public function testCheckboxGroupWithValueAndOldInput()
{
$oldInput = Mockery::mock('Laravolt\SemanticForm\OldInput\OldInputInterface');
$oldInput->shouldReceive('hasOldInput')->andReturn(true);
$oldInput->shouldReceive('getOldInput')->with('fruit')->andReturn(['orange' => 'orange']);

$this->form->setOldInputProvider($oldInput);

$expected = '<div class="grouped fields">';
$expected .= '<label>Fruit</label>';
$expected .= '<div class="field">';
$expected .= '<div class="ui checkbox">';
$expected .= '<input type="checkbox" name="fruit[orange]" value="orange" checked="checked">';
$expected .= '<label>Orange</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '<div class="field">';
$expected .= '<div class="ui checkbox">';
$expected .= '<input type="checkbox" name="fruit[banana]" value="banana">';
$expected .= '<label>Banana</label>';
$expected .= '</div>';
$expected .= '</div>';
$expected .= '</div>';

$options = ['orange' => 'Orange', 'banana'=>'Banana'];
$result = (string)$this->form->checkboxGroup('fruit', $options, ['banana'])->label('Fruit');
$this->assertEquals($expected, $result);

$result = (string)$this->form->checkboxGroup('fruit', $options, 'banana')->label('Fruit');
$this->assertEquals($expected, $result);
}

public function testCheckboxGroupInline()
{
$expected = '<div class="inline fields">';
Expand Down

0 comments on commit 772ee10

Please sign in to comment.