From 906de4e6792c1be2c364e6ca1df5e6329da091ba Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 3 Sep 2014 14:39:42 +0200 Subject: [PATCH] lib: Add the HTML5 attributes 'min', 'max' and 'step' to the number input control refs #6593 --- application/views/helpers/FormNumber.php | 42 ++++++++- library/Icinga/Web/Form/Element/Number.php | 103 ++++++++++++++++++++- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/application/views/helpers/FormNumber.php b/application/views/helpers/FormNumber.php index 98cb8d06f..c3ccdcfd6 100644 --- a/application/views/helpers/FormNumber.php +++ b/application/views/helpers/FormNumber.php @@ -9,6 +9,26 @@ use \Zend_View_Helper_FormElement; */ class Zend_View_Helper_FormNumber extends Zend_View_Helper_FormElement { + /** + * Format a number + * + * @param $number + * + * @return string + */ + public function formatNumber($number) + { + if (empty($number)) { + return $number; + } + return $this->view->escape( + sprintf( + ctype_digit((string) $number) ? '%d' : '%F', + $number + ) + ); + } + /** * Render the number input control * @@ -28,11 +48,29 @@ class Zend_View_Helper_FormNumber extends Zend_View_Helper_FormElement if ($disable) { $disabled = ' disabled="disabled"'; } + $min = ''; + if (isset($attribs['min'])) { + $min = sprintf(' min="%s"', $this->formatNumber($attribs['min'])); + } + unset($attribs['min']); // Unset min to not render it again in $this->_htmlAttribs($attribs) + $max = ''; + if (isset($attribs['max'])) { + $max = sprintf(' max="%s"', $this->formatNumber($attribs['max'])); + } + unset($attribs['max']); // Unset max to not render it again in $this->_htmlAttribs($attribs) + $step = ''; + if (isset($attribs['step'])) { + $step = sprintf(' step="%s"', $attribs['step'] === 'any' ? 'any' : $this->formatNumber($attribs['step'])); + } + unset($attribs['step']); // Unset step to not render it again in $this->_htmlAttribs($attribs) $html5 = sprintf( - 'view->escape($name), $this->view->escape($id), - $this->view->escape($value), + $this->view->escape($this->formatNumber($value)), + $min, + $max, + $step, $disabled, $this->_htmlAttribs($attribs), $this->getClosingBracket() diff --git a/library/Icinga/Web/Form/Element/Number.php b/library/Icinga/Web/Form/Element/Number.php index 3bfbc6406..75c08165c 100644 --- a/library/Icinga/Web/Form/Element/Number.php +++ b/library/Icinga/Web/Form/Element/Number.php @@ -29,12 +29,113 @@ class Number extends Zend_Form_Element */ public $helper = 'formNumber'; + /** + * The expected lower bound for the element’s value + * + * @var float|null + */ + protected $min; + + /** + * The expected upper bound for the element’s + * + * @var float|null + */ + protected $max; + + /** + * The value granularity of the element’s value + * + * Normally, number input controls are limited to an accuracy of integer values. + * + * @var float|string|null + */ + protected $step; + /** * (non-PHPDoc) * @see \Zend_Form_Element::init() For the method documentation. */ public function init() { - $this->addValidator('Int'); + $this->addValidator('Float', true); // true for breaking the validator chain on failure + if ($this->min !== null) { + $this->addValidator('GreaterThan', true, array('min' => $this->min)); + } + if ($this->max !== null) { + $this->addValidator('LessThan', true, array('max' => $this->max)); + } + } + + /** + * Set the expected lower bound for the element’s value + * + * @param float $min + * + * @return $this + */ + public function setMin($min) + { + $this->min = (float) $min; + return $this; + } + + /** + * Get the expected lower bound for the element’s value + * + * @return float|null + */ + public function getMin() + { + return $this->min; + } + + /** + * Set the expected upper bound for the element’s value + * + * @param float $max + * + * @return $this + */ + public function setMax($max) + { + $this->max = (int) $max; + return $this; + } + + /** + * Get the expected upper bound for the element’s value + * + * @return float|null + */ + public function getMax() + { + return $this->max; + } + + /** + * Set the value granularity of the element’s value + * + * @param float|string $step + * + * @return $this + */ + public function setStep($step) + { + if ($step !== 'any') { + $step = (float) $step; + } + $this->step = $step; + return $this; + } + + /** + * Get the value granularity of the element’s value + * + * @return float|string|null + */ + public function getStep() + { + return $this->step; } }