lib: Add the HTML5 attributes 'min', 'max' and 'step' to the number input control

refs #6593
This commit is contained in:
Eric Lippmann 2014-09-03 14:39:42 +02:00
parent de11131822
commit 906de4e679
2 changed files with 142 additions and 3 deletions

View File

@ -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(
'<input type="number" name="%s" id="%s" value="%s"%s%s%s',
'<input type="number" name="%s" id="%s" value="%s"%s%s%s%s%s%s',
$this->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()

View File

@ -29,12 +29,113 @@ class Number extends Zend_Form_Element
*/
public $helper = 'formNumber';
/**
* The expected lower bound for the elements value
*
* @var float|null
*/
protected $min;
/**
* The expected upper bound for the elements
*
* @var float|null
*/
protected $max;
/**
* The value granularity of the elements 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 elements value
*
* @param float $min
*
* @return $this
*/
public function setMin($min)
{
$this->min = (float) $min;
return $this;
}
/**
* Get the expected lower bound for the elements value
*
* @return float|null
*/
public function getMin()
{
return $this->min;
}
/**
* Set the expected upper bound for the elements value
*
* @param float $max
*
* @return $this
*/
public function setMax($max)
{
$this->max = (int) $max;
return $this;
}
/**
* Get the expected upper bound for the elements value
*
* @return float|null
*/
public function getMax()
{
return $this->max;
}
/**
* Set the value granularity of the elements 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 elements value
*
* @return float|string|null
*/
public function getStep()
{
return $this->step;
}
}