1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Field\Base\AbstractInputField; |
9
|
|
|
use Yiisoft\Form\Field\Base\PlaceholderTrait; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A control for setting the element's value to a string representing a number. |
14
|
|
|
* |
15
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number) |
16
|
|
|
*/ |
17
|
|
|
final class Number extends AbstractInputField |
18
|
|
|
{ |
19
|
|
|
use PlaceholderTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-max |
23
|
|
|
*/ |
24
|
|
|
public function max(?string $value): self |
25
|
|
|
{ |
26
|
|
|
$new = clone $this; |
27
|
|
|
$new->inputTagAttributes['max'] = $value; |
28
|
|
|
return $new; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min |
33
|
|
|
*/ |
34
|
|
|
public function min(?string $value): self |
35
|
|
|
{ |
36
|
|
|
$new = clone $this; |
37
|
|
|
$new->inputTagAttributes['min'] = $value; |
38
|
|
|
return $new; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
43
|
|
|
* |
44
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
45
|
|
|
* |
46
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
47
|
|
|
*/ |
48
|
|
|
public function readonly(bool $value = true): self |
49
|
|
|
{ |
50
|
|
|
$new = clone $this; |
51
|
|
|
$new->inputTagAttributes['readonly'] = $value; |
52
|
|
|
return $new; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* A boolean attribute. When specified, the element is required. |
57
|
|
|
* |
58
|
|
|
* @param bool $value Whether the control is required for form submission. |
59
|
|
|
* |
60
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
61
|
|
|
*/ |
62
|
|
|
public function required(bool $value = true): self |
63
|
|
|
{ |
64
|
|
|
$new = clone $this; |
65
|
|
|
$new->inputTagAttributes['required'] = $value; |
66
|
|
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
protected function generateInput(): string |
70
|
|
|
{ |
71
|
1 |
|
$value = $this->getAttributeValue(); |
72
|
|
|
|
73
|
1 |
|
if (!is_numeric($value) && $value !== null) { |
74
|
|
|
throw new InvalidArgumentException('Number widget must be a numeric or null value.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$tagAttributes = $this->getInputTagAttributes(); |
78
|
|
|
|
79
|
1 |
|
return Html::input('number', $this->getInputName(), $value, $tagAttributes)->render(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|