-
-
Notifications
You must be signed in to change notification settings - Fork 151
Description
Version: 3.*
Problem description
For some reason, CheckBox, HiddenField and Button have no label by default. But the other inputs do have labels.

What If we did want our checkboxes to actually have a label? And render it like a label of the other elements? (For design consistency or any other reason.)

Of course I know manual rendering could be used. But why?
Or we wanted to make it Bootstrap-compatible (render the input outside of <label> tag)?
If anyone could make Nette forms use their own implementation of Checkbox ... but we can't, since in order to use addCheckbox method we need to return an instance of Nette\Forms\Control\Checkbox and since its getLabel method is defined as public function getLabel($caption = null): void there is no way to force any other return type on it...
Possible solution
- drop
voidreturn type onCheckbox::getLabelmethod. - implement a mechanism to allow users to be able to select how they want the label be rendered.
In Nette 2.4 we have solved this elegantly this way (one is able to chose from 3 different styles of how getControl and getLabel return their values):
public function getControl()
{
$flag = $this->getLabelRenderFlag();
// label and control wrapped together
if ($flag === self::LABEL_NETTE_STYLE) {
return $this->parent_getControl();
}
// wrap label and label separately
if ($flag === self::LABEL_SEPARATE_WRAPPER) {
$this->getSeparatorPrototype()->addHtml($this->getControlPart());
$this->getSeparatorPrototype()->addHtml($this->getLabelPart());
return $this->getSeparatorPrototype();
}
// only control part returned (label is rendered separately)
return $this->getSeparatorPrototype()->addHtml($this->getControlPart());
}
public function getLabel($caption = null)
{
// label is not rendered
if ($this->getLabelRenderFlag() !== self::LABEL_STANDALONE) {
return parent::getLabel($caption);
}
// render label separately
return $this->getLabelPart();
}I'm open to providing a PR for the above code.
Please also consider dropping the return type of the other methods, for consistency sake, even though it makes less sense to override labels of these methods:
RadioList::getLabelHiddenField::getLabelButton::getLabel