lib: Add the HTML5 attributes 'min', 'max' and 'step' to the number input control
refs #6593
This commit is contained in:
parent
de11131822
commit
906de4e679
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue