Skip to content
Open
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
31 changes: 30 additions & 1 deletion src/Helper/Input/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
*/
class Checkbox extends AbstractChecked
{

/**
*
* Individual attributes for each option (only for multi checkbox)
*
* @var array
*
*/
protected $optionsAttribs = array();
Copy link
Member

Choose a reason for hiding this comment

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

There is also a dilemma in this naming. If I am correct in v2 we have used underscores. So there is a consistency issue I guess. We can revisit this later.


/**
*
* Prepares the properties on this helper.
*
* @param array $spec The specification array.
*
*/
protected function prep(array $spec)
{
if (isset($spec['optionsAttribs'])) {
$this->optionsAttribs = $spec['optionsAttribs'];
unset($spec['optionsAttribs']);
}

parent::prep($spec);
}

/**
*
* Returns the HTML for the input.
Expand Down Expand Up @@ -80,10 +107,12 @@ protected function multiple()
$this->attribs['value'] = $value;
$this->attribs['label'] = $label;

$optionAttribs = isset($this->optionsAttribs[$value]) ? $this->optionsAttribs[$value] : [];
Copy link
Member

Choose a reason for hiding this comment

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

Travis broken due to 5.3 . Need to change short array to array.


$html .= $checkbox(array(
'name' => $this->attribs['name'],
'value' => $this->value,
'attribs' => $this->attribs
'attribs' => $this->attribs+$optionAttribs
Copy link
Member

Choose a reason for hiding this comment

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

a spacing is also nice for readability :) .

));
}
return $html;
Expand Down
24 changes: 24 additions & 0 deletions tests/Helper/Input/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,28 @@ public function testMultiCheckboxWithValues() {
. '<label><input type="checkbox" name="foo[]" value="maybe" /> Maybe</label>' . PHP_EOL;
$this->assertSame($expect, $actual);
}

public function testOptionAttributes() {
$checkbox = $this->helper;
$actual = $checkbox(array(
'name' => 'foo',
'value' => 'yes',
'options' => array(
'yes' => 'Yes',
'no' => 'No',
),
'optionsAttribs' => array(
'yes' => array(
'class' => 'test-class'
),
'no' => array(
'data-no' => 1
),
),
))->__toString();
$expect = '<label><input type="checkbox" name="foo[]" value="yes" class="test-class" checked /> Yes</label>' . PHP_EOL
. '<label><input type="checkbox" name="foo[]" value="no" data-no="1" /> No</label>' . PHP_EOL;
$this->assertSame($expect, $actual);
}

}