1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\Attribute; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Widget\Validator\FieldValidator; |
8
|
|
|
|
9
|
|
|
abstract class InputAttributes extends WidgetAttributes |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Set aria-describedby attribute. |
13
|
|
|
* |
14
|
|
|
* @param string $value |
15
|
|
|
* |
16
|
|
|
* @return static |
17
|
|
|
* |
18
|
|
|
* @link https://www.w3.org/TR/WCAG20-TECHS/ARIA1.html |
19
|
|
|
*/ |
20
|
1 |
|
public function ariaDescribedBy(string $value): self |
21
|
|
|
{ |
22
|
1 |
|
$new = clone $this; |
23
|
1 |
|
$new->attributes['aria-describedby'] = $value; |
24
|
1 |
|
return $new; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set aria-label attribute. |
29
|
|
|
* |
30
|
|
|
* @param string $value |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
|
|
public function ariaLabel(string $value): self |
35
|
|
|
{ |
36
|
|
|
$new = clone $this; |
37
|
|
|
$new->attributes['aria-label'] = $value; |
38
|
|
|
return $new; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id |
43
|
|
|
* attribute of a {@see Form} element in the same document. |
44
|
|
|
* |
45
|
|
|
* @param string $value |
46
|
|
|
* |
47
|
|
|
* @return static |
48
|
|
|
* |
49
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
50
|
|
|
*/ |
51
|
|
|
public function form(string $value): self |
52
|
|
|
{ |
53
|
|
|
$new = clone $this; |
54
|
|
|
$new->attributes['form'] = $value; |
55
|
|
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* A Boolean attribute which, if present, means this field cannot be edited by the user. |
60
|
|
|
* Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value |
61
|
|
|
* property. |
62
|
|
|
* |
63
|
|
|
* @param bool $value |
64
|
|
|
* |
65
|
|
|
* @return static |
66
|
|
|
* |
67
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute |
68
|
|
|
*/ |
69
|
14 |
|
public function readonly(bool $value = true): self |
70
|
|
|
{ |
71
|
14 |
|
$new = clone $this; |
72
|
14 |
|
$new->attributes['readonly'] = $value; |
73
|
14 |
|
return $new; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* If it is required to fill in a value in order to submit the form. |
78
|
|
|
* |
79
|
|
|
* @return static |
80
|
|
|
* |
81
|
|
|
* @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute |
82
|
|
|
*/ |
83
|
13 |
|
public function required(): self |
84
|
|
|
{ |
85
|
13 |
|
$new = clone $this; |
86
|
13 |
|
$new->attributes['required'] = true; |
87
|
13 |
|
return $new; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set build attributes for the widget. |
92
|
|
|
* |
93
|
|
|
* @param array $attributes $value |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
535 |
|
protected function build(array $attributes): array |
98
|
|
|
{ |
99
|
535 |
|
if (!array_key_exists('id', $attributes)) { |
100
|
486 |
|
$attributes['id'] = $this->getInputId(); |
101
|
|
|
} |
102
|
|
|
|
103
|
535 |
|
if (!array_key_exists('name', $attributes)) { |
104
|
483 |
|
$attributes['name'] = $this->getInputName(); |
105
|
|
|
} |
106
|
|
|
|
107
|
535 |
|
$fieldValidator = new FieldValidator(); |
108
|
|
|
|
109
|
535 |
|
return $fieldValidator->getValidatorAttributes( |
110
|
535 |
|
$this, |
111
|
535 |
|
$this->getFormModel(), |
112
|
535 |
|
$this->getAttribute(), |
113
|
|
|
$attributes, |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|