summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Form/Element/Number.php
blob: afbd07df655e1d15129d64fc8e6f564dcbbf4a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Form\Element;

use Icinga\Web\Form\FormElement;

/**
 * A number input control
 */
class Number extends FormElement
{
    /**
     * Form view helper to use for rendering
     *
     * @var string
     */
    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()
    {
        if ($this->min !== null || $this->max !== null) {
            $this->addValidator('Between', true, array(
                'min'       => $this->min === null ? -INF : $this->min,
                'max'       => $this->max === null ? INF : $this->max,
                'inclusive' => true
            ));
        }
    }

    /**
     * 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 = (float) $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;
    }

    /**
     * (non-PHPDoc)
     * @see \Zend_Form_Element::isValid() For the method documentation.
     */
    public function isValid($value, $context = null)
    {
        $this->setValue($value);
        $value = $this->getValue();
        if ($value !== null && $value !== '' && ! is_numeric($value)) {
            $this->addError(sprintf(t('\'%s\' is not a valid number'), $value));
            return false;
        }
        return parent::isValid($value, $context);
    }
}