|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Input element for integer value. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Formgenerator |
|
10
|
|
|
* @author Stefanius <[email protected]> |
|
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
12
|
|
|
*/ |
|
13
|
|
|
class FormInt extends FormInput |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var bool empty entries allowed. If false, empty input is set to '0' */ |
|
16
|
|
|
protected bool $bEmptyAllowed = false; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $strName name of input |
|
20
|
|
|
* @param int $iSize size in digits |
|
21
|
|
|
* @param int $wFlags flags (default = 0) |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(string $strName, int $iSize, int $wFlags = 0) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($strName, $iSize, $wFlags); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* We 'ask' the configuration for alignment. |
|
30
|
|
|
* $this->oFG is not available until the parent is set! |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function onParentSet() : void |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->oFG->getConfig()->getBool('Integer.RightAlign', true)) { |
|
35
|
|
|
$this->addFlags(FormFlags::ALIGN_RIGHT); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// if readonly, don't set the 'number' type... |
|
39
|
|
|
if (!$this->oFlags->isSet(FormFlags::READ_ONLY)) { |
|
40
|
|
|
$this->strType = 'number'; |
|
41
|
|
|
$this->addStyle('width', $this->size . 'em'); |
|
42
|
|
|
} |
|
43
|
|
|
$this->addAttribute('data-validation', 'integer:' . ($this->bEmptyAllowed ? 'e' : 'x')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set min/max value accepted by input field. |
|
48
|
|
|
* @param int $iMin - set to null, if no limitation wanted |
|
49
|
|
|
* @param int $iMax - set to null, if no limitation wanted |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setMinMax(?int $iMin, ?int $iMax) : void |
|
52
|
|
|
{ |
|
53
|
|
|
if ($iMin !== null) { |
|
54
|
|
|
$this->addAttribute('min', (string)$iMin); |
|
55
|
|
|
} |
|
56
|
|
|
if ($iMax !== null) { |
|
57
|
|
|
$this->addAttribute('max', (string)$iMax); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function buildValue() : string |
|
66
|
|
|
{ |
|
67
|
|
|
$iValue = intval($this->oFG->getData()->getValue($this->strName)); |
|
68
|
|
|
if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $iValue == 0) { |
|
69
|
|
|
return ''; |
|
70
|
|
|
} |
|
71
|
|
|
$strHTML = ' value="' . $iValue . '"'; |
|
72
|
|
|
|
|
73
|
|
|
return $strHTML; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|