Skip to content

Commit

Permalink
Form: more flexible label handling #1312
Browse files Browse the repository at this point in the history
You now can add labels that don't wrap around inputs, but you have to
ensure IDs are properly assigned yourself.

The Label class has been renamed to LabelElement to reflect the naming
scheme of the other elements.
  • Loading branch information
splitbrain committed Aug 18, 2015
1 parent 8f0df22 commit a453c16
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
35 changes: 35 additions & 0 deletions inc/Form/Form.php
Expand Up @@ -257,6 +257,41 @@ public function addButtonHTML($name, $html, $pos = -1) {
return $this->addElement(new ButtonElement($name, $html), $pos);
}

/**
* Adds a label referencing another input element, escapes the label for you
*
* @param $label
* @param string $for
* @param int $pos
* @return Element
*/
public function addLabel($label, $for='', $pos = -1) {
return $this->addLabelHTML(hsc($label), $for, $pos);
}

/**
* Adds a label referencing another input element, allows HTML for content
*
* @param string $content
* @param string|Element $for
* @param int $pos
* @return Element
*/
public function addLabelHTML($content, $for='', $pos = -1) {
$element = new LabelElement(hsc($content));

if(is_a($for, '\dokuwiki\Form\Element')) {
/** @var Element $for */
$for = $for->id();
}
$for = (string) $for;
if($for !== '') {
$element->attr('for', $for);
}

return $this->addElement($element, $pos);
}

/**
* Add fixed HTML to the form
*
Expand Down
8 changes: 4 additions & 4 deletions inc/Form/InputElement.php
Expand Up @@ -12,7 +12,7 @@
*/
class InputElement extends Element {
/**
* @var Label
* @var LabelElement
*/
protected $label = null;

Expand All @@ -24,19 +24,19 @@ class InputElement extends Element {
/**
* @param string $type The type of this element
* @param string $name The name of this form element
* @param string $label The label text for this element
* @param string $label The label text for this element (will be autoescaped)
*/
public function __construct($type, $name, $label = '') {
parent::__construct($type, array('name' => $name));
$this->attr('name', $name);
$this->attr('type', $type);
if($label) $this->label = new Label($label);
if($label) $this->label = new LabelElement($label);
}

/**
* Returns the label element if there's one set
*
* @return Label|null
* @return LabelElement|null
*/
public function getLabel() {
return $this->label;
Expand Down
6 changes: 3 additions & 3 deletions inc/Form/Label.php → inc/Form/LabelElement.php
Expand Up @@ -5,12 +5,12 @@
* Class Label
* @package dokuwiki\Form
*/
class Label extends ValueElement {
class LabelElement extends ValueElement {

/**
* Creates a new Label
*
* @param string $label
* @param string $label This is is raw HTML and will not be escaped
*/
public function __construct($label) {
parent::__construct('label', $label);
Expand All @@ -22,6 +22,6 @@ public function __construct($label) {
* @return string
*/
public function toHTML() {
return '<label ' . buildAttributes($this->attrs()) . '>' . hsc($this->val()) . '</label>';
return '<label ' . buildAttributes($this->attrs()) . '>' . $this->val() . '</label>';
}
}

0 comments on commit a453c16

Please sign in to comment.