Skip to content
Permalink
Browse files

CheckboxList: added containerPrototype and itemLabelPrototype [Closes #…

  • Loading branch information...
enumag authored and dg committed Aug 9, 2015
1 parent 5b393ff commit e0bc505f92743a0a6cd1dae94b31426b3b898538
Showing with 93 additions and 33 deletions.
  1. +60 −23 src/Forms/Controls/CheckboxList.php
  2. +33 −10 tests/Forms/Controls.CheckboxList.render.phpt
@@ -15,41 +15,58 @@
* Set of checkboxes.
*
* @property-read Html $separatorPrototype
* @property-read Html $containerPrototype
* @property-read Html $itemLabelPrototype
*/
class CheckboxList extends MultiChoiceControl
{
/** @var Html separator element template */
protected $separator;
/** @var Html container element template */
protected $container;
/** @var Html item label template */
protected $itemLabel;
/**
* @param string label
* @param array options from which to choose
*/
public function __construct($label = NULL, array $items = NULL)
{
parent::__construct($label, $items);
$this->control->type = 'checkbox';
$this->container = Html::el();
$this->separator = Html::el('br');
$this->itemLabel = Html::el();
}
/**
* Generates control's HTML element.
* @return string
* @return Html
*/
public function getControl()
{
$input = parent::getControl();
$items = $this->getItems();
reset($items);
$input = parent::getControl();
return Nette\Forms\Helpers::createInputList(
$this->translate($items),
array_merge($input->attrs, array(
'id' => NULL,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'required' => NULL,
'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
)),
$this->label->attrs,
$this->separator
return $this->container->setHtml(
Nette\Forms\Helpers::createInputList(
$this->translate($items),
array_merge($input->attrs, array(
'id' => NULL,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'required' => NULL,
'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
)),
$this->itemLabel->attrs,
$this->separator
)
);
}
@@ -65,16 +82,6 @@ public function getLabel($caption = NULL)
}
/**
* Returns separator HTML element template.
* @return Html
*/
public function getSeparatorPrototype()
{
return $this->separator;
}
/**
* @return Html
*/
@@ -101,4 +108,34 @@ public function getLabelPart($key = NULL)
: $this->getLabel();
}
/**
* Returns separator HTML element template.
* @return Html
*/
public function getSeparatorPrototype()
{
return $this->separator;
}
/**
* Returns container HTML element template.
* @return Html
*/
public function getContainerPrototype()
{
return $this->container;
}
/**
* Returns item label HTML element template.
* @return Html
*/
public function getItemLabelPrototype()
{
return $this->itemLabel;
}
}
@@ -35,8 +35,8 @@ test(function () {
Assert::type('Nette\Utils\Html', $input->getLabelPart(0));
Assert::same('<label for="frm-list-0">Second</label>', (string) $input->getLabelPart(0));
Assert::type('string', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
Assert::type('Nette\Utils\Html', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::type('Nette\Utils\Html', $input->getControlPart(0));
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
@@ -50,7 +50,7 @@ test(function () { // checked
0 => 'Second',
))->setValue(0);
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', (string) $input->getControl());
});
@@ -66,7 +66,7 @@ test(function () { // translator
Assert::same('<label>ANOTHER LABEL</label>', (string) $input->getLabel('Another label'));
Assert::same('<label for="frm-list-0">SECOND</label>', (string) $input->getLabelPart(0));
Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
});
@@ -81,7 +81,7 @@ test(function () { // Html
Assert::same('<label><b>Label</b></label>', (string) $input->getLabel());
Assert::same('<label><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" value="a">', (string) $input->getControlPart('a'));
});
@@ -93,7 +93,7 @@ test(function () { // validation rules
0 => 'Second',
))->setRequired('required');
Assert::same('<label><input type="checkbox" name="list[]" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="0">', (string) $input->getControlPart(0));
});
@@ -106,7 +106,7 @@ test(function () { // container
0 => 'Second',
));
Assert::same('<label><input type="checkbox" name="container[list][]" value="a">First</label><br><label><input type="checkbox" name="container[list][]" value="0">Second</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="container[list][]" value="a">First</label><br><label><input type="checkbox" name="container[list][]" value="0">Second</label>', (string) $input->getControl());
});
@@ -117,7 +117,7 @@ test(function () { // separator prototype
));
$input->getSeparatorPrototype()->setName('div');
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', $input->getControl());
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
});
@@ -128,7 +128,7 @@ test(function () { // disabled all
0 => 'Second',
))->setDisabled(TRUE);
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" disabled value="0">Second</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" disabled value="0">Second</label>', (string) $input->getControl());
});
@@ -139,7 +139,7 @@ test(function () { // disabled one
0 => 'Second',
))->setDisabled(array('a'));
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
});
@@ -153,3 +153,26 @@ test(function () { // numeric key as string & getControlPart
Assert::same('<input type="checkbox" name="list[]" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
});
test(function () { // container prototype
$form = new Form;
$input = $form->addCheckboxList('list', NULL, array(
'a' => 'b',
));
$input->getSeparatorPrototype()->setName('hr');
$input->getContainerPrototype()->setName('div');
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
});
test(function () { // item label prototype
$form = new Form;
$input = $form->addCheckboxList('list', NULL, array(
'a' => 'b',
));
$input->getItemLabelPrototype()->class('foo');
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
});

0 comments on commit e0bc505

Please sign in to comment.
You can’t perform that action at this time.