Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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 $options_attribs = array();

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

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->options_attribs[$value]) ? $this->options_attribs[$value] : array();
Copy link
Member

Choose a reason for hiding this comment

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

Probably here also :) .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


$html .= $checkbox(array(
'name' => $this->attribs['name'],
'value' => $this->value,
'attribs' => $this->attribs
'attribs' => $this->attribs + $optionAttribs
));
}
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',
),
'options_attribs' => 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);
}

}