From 08099e4fe1e56308bc42cc639d187863088494bd Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 18 Aug 2015 19:28:45 +0200 Subject: Form: correctly set type attribute for inputs #1312 --- inc/Form/InputElement.php | 1 + 1 file changed, 1 insertion(+) (limited to 'inc/Form') diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 5908f7d11..693eeffc5 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -29,6 +29,7 @@ class InputElement extends Element { 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); } -- cgit v1.2.3 From 8f0df229ed3e82191f118594ea5145c9d5942d7b Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 18 Aug 2015 20:12:32 +0200 Subject: Form: added Button element #1312 --- inc/Form/ButtonElement.php | 34 ++++++++++++++++++++++++++++++++++ inc/Form/Form.php | 24 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 inc/Form/ButtonElement.php (limited to 'inc/Form') diff --git a/inc/Form/ButtonElement.php b/inc/Form/ButtonElement.php new file mode 100644 index 000000000..77c30ed4f --- /dev/null +++ b/inc/Form/ButtonElement.php @@ -0,0 +1,34 @@ + $name, 'value' => 1)); + $this->content = $content; + } + + /** + * The HTML representation of this element + * + * @return string + */ + public function toHTML() { + return ''; + } + +} diff --git a/inc/Form/Form.php b/inc/Form/Form.php index 625557fa1..738f2bcf8 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -233,6 +233,30 @@ class Form extends Element { return $this->addElement(new TextareaElement($name, $label), $pos); } + /** + * Adds a simple button, escapes the content for you + * + * @param string $name + * @param string $content + * @param int $pos + * @return Element + */ + public function addButton($name, $content, $pos = -1) { + return $this->addElement(new ButtonElement($name, hsc($content)), $pos); + } + + /** + * Adds a simple button, allows HTML for content + * + * @param string $name + * @param string $html + * @param int $pos + * @return Element + */ + public function addButtonHTML($name, $html, $pos = -1) { + return $this->addElement(new ButtonElement($name, $html), $pos); + } + /** * Add fixed HTML to the form * -- cgit v1.2.3 From a453c16b3290eabdf35778c54498101c737544e1 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 18 Aug 2015 20:36:08 +0200 Subject: Form: more flexible label handling #1312 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. --- inc/Form/Form.php | 35 +++++++++++++++++++++++++++++++++++ inc/Form/InputElement.php | 8 ++++---- inc/Form/Label.php | 27 --------------------------- inc/Form/LabelElement.php | 27 +++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 31 deletions(-) delete mode 100644 inc/Form/Label.php create mode 100644 inc/Form/LabelElement.php (limited to 'inc/Form') diff --git a/inc/Form/Form.php b/inc/Form/Form.php index 738f2bcf8..73b5b9cb3 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -257,6 +257,41 @@ class Form extends Element { 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 * diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 693eeffc5..694dd0848 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -12,7 +12,7 @@ namespace dokuwiki\Form; */ class InputElement extends Element { /** - * @var Label + * @var LabelElement */ protected $label = null; @@ -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; diff --git a/inc/Form/Label.php b/inc/Form/Label.php deleted file mode 100644 index 8dcd7cd5f..000000000 --- a/inc/Form/Label.php +++ /dev/null @@ -1,27 +0,0 @@ -attrs()) . '>' . hsc($this->val()) . ''; - } -} diff --git a/inc/Form/LabelElement.php b/inc/Form/LabelElement.php new file mode 100644 index 000000000..9c8d54277 --- /dev/null +++ b/inc/Form/LabelElement.php @@ -0,0 +1,27 @@ +attrs()) . '>' . $this->val() . ''; + } +} -- cgit v1.2.3 From bb6d40dcf2a7c8fde5be24560ee47fd566d2b201 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 18 Aug 2015 20:42:11 +0200 Subject: Form: fixed class checks --- inc/Form/Form.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'inc/Form') diff --git a/inc/Form/Form.php b/inc/Form/Form.php index 73b5b9cb3..7eaa53041 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -140,7 +140,7 @@ class Form extends Element { * @return Element */ public function addElement(Element $element, $pos = -1) { - if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); + if(is_a($element, '\dokuwiki\Form\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); if($pos < 0) { $this->elements[] = $element; } else { @@ -156,7 +156,7 @@ class Form extends Element { * @param $pos 0-based position of the element to replace */ public function replaceElement(Element $element, $pos) { - if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); + if(is_a($element, '\dokuwiki\Form\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); array_splice($this->elements, $pos, 1, array($element)); } -- cgit v1.2.3