From 12a4e4d1ed827c59290838d5a11d75ad32aa28f1 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 16:15:16 +0200 Subject: start of rewriting the form handling This is jsut a small beginning. Not all elements are there, yet. It's also completely untested so far. --- inc/Form/InputElement.php | 145 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 inc/Form/InputElement.php (limited to 'inc/Form/InputElement.php') diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php new file mode 100644 index 000000000..59310174e --- /dev/null +++ b/inc/Form/InputElement.php @@ -0,0 +1,145 @@ + $name)); + $this->attr('name', $name); + } + + /** + * Should the user sent input be used to initialize the input field + * + * The default is true. Any set values will be overwritten by the INPUT + * provided values. + * + * @param bool $useinput + * @return $this + */ + public function useInput($useinput) { + $this->useInput = (bool) $useinput; + return $this; + } + + /** + * Get or set the element's ID + * + * @param null|string $id + * @return string|$this + */ + public function id($id = null) { + $this->label->attr('for', $id); + return parent::id($id); + } + + /** + * Adds a class to the class attribute + * + * This is the preferred method of setting the element's class + * + * @param string $class the new class to add + * @return $this + */ + public function addClass($class) { + $this->label->addClass($class); + return parent::addClass($class); + } + + /** + * Figures out how to access the value for this field from INPUT data + * + * The element's name could have been given as a simple string ('foo') + * or in array notation ('foo[bar]'). + * + * Note: this function only handles one level of arrays. If your data + * is nested deeper, you should call useInput(false) and set the + * correct value yourself + * + * @return array name and array key (null if not an array) + */ + protected function getInputName() { + $name = $this->attr('name'); + parse_str("$name=1", $parsed); + + $name = array_keys($parsed); + $name = array_shift($name); + + if(is_array($parsed[$name])) { + $key = array_keys($parsed[$name]); + $key = array_shift($key); + } else { + $key = null; + } + + return array($name, $key); + } + + /** + * Handles the useInput flag and set the value attribute accordingly + */ + protected function prefillInput() { + global $INPUT; + + list($name, $key) = $this->getInputName(); + if(!$INPUT->has($name)) return; + + if($key === null) { + $value = $INPUT->str($name); + } else { + $value = $INPUT->arr($name); + if(isset($value[$key])) { + $value = $value[$key]; + } else { + $value = ''; + } + } + if($value !== '') { + $this->val($value); + } + } + + /** + * The HTML representation of this element + * + * @return string + */ + protected function mainElementHTML() { + if($this->useInput) $this->prefillInput(); + return 'attrs()) . ' />'; + } + + /** + * The HTML representation of this element wrapped in a label + * + * @return string + */ + public function toHTML() { + return ''; + } +} -- cgit v1.2.3 From 6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 17:26:11 +0200 Subject: added a first few tests. this is far from comprehensible, but should give an idea how the new library works and how to write tests --- inc/Form/InputElement.php | 1 + 1 file changed, 1 insertion(+) (limited to 'inc/Form/InputElement.php') diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 59310174e..4f644c0f1 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -28,6 +28,7 @@ class InputElement extends Element { public function __construct($type, $name, $label) { parent::__construct($type, array('name' => $name)); $this->attr('name', $name); + $this->label = new Label($label); } /** -- cgit v1.2.3 From de19515f04567db78bd41d5bff68a88bfb8c2a22 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 19:12:21 +0200 Subject: started with the compatibility layer --- inc/Form/InputElement.php | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'inc/Form/InputElement.php') diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 4f644c0f1..939d911c1 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -4,7 +4,8 @@ namespace dokuwiki\Form; /** * Class InputElement * - * Base class for all input elements. Uses a wrapping label. + * Base class for all input elements. Uses a wrapping label when label + * text is given. * * @todo figure out how to make wrapping or related label configurable * @package dokuwiki\Form @@ -13,7 +14,7 @@ class InputElement extends Element { /** * @var Label */ - protected $label; + protected $label = null; /** * @var bool if the element should reflect posted values @@ -25,10 +26,19 @@ class InputElement extends Element { * @param string $name The name of this form element * @param string $label The label text for this element */ - public function __construct($type, $name, $label) { + public function __construct($type, $name, $label = '') { parent::__construct($type, array('name' => $name)); $this->attr('name', $name); - $this->label = new Label($label); + if($label) $this->label = new Label($label); + } + + /** + * Returns the label element if there's one set + * + * @return Label|null + */ + public function getLabel() { + return $this->label; } /** @@ -52,7 +62,7 @@ class InputElement extends Element { * @return string|$this */ public function id($id = null) { - $this->label->attr('for', $id); + if($this->label) $this->label->attr('for', $id); return parent::id($id); } @@ -65,7 +75,7 @@ class InputElement extends Element { * @return $this */ public function addClass($class) { - $this->label->addClass($class); + if($this->label) $this->label->addClass($class); return parent::addClass($class); } @@ -138,9 +148,13 @@ class InputElement extends Element { * @return string */ public function toHTML() { - return ''; + if($this->label) { + return ''; + } else { + return $this->mainElementHTML(); + } } } -- cgit v1.2.3 From 370c645068595c6750cee2b38eb96aeaac3a9dbb Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 11 May 2015 19:52:12 +0200 Subject: fixed label output --- inc/Form/InputElement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/Form/InputElement.php') diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 939d911c1..5908f7d11 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -150,7 +150,7 @@ class InputElement extends Element { public function toHTML() { if($this->label) { return ''; } else { -- cgit v1.2.3