1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Widget\Attribute\InputAttributes; |
9
|
|
|
use Yiisoft\Form\Widget\Attribute\PlaceholderInterface; |
10
|
|
|
use Yiisoft\Form\Widget\Validator\HasLengthInterface; |
11
|
|
|
use Yiisoft\Form\Widget\Validator\MatchRegularInterface; |
12
|
|
|
use Yiisoft\Html\Tag\Textarea as TextAreaTag; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generates a textarea tag for the given form attribute. |
16
|
|
|
* |
17
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html |
18
|
|
|
*/ |
19
|
|
|
final class TextArea extends InputAttributes implements HasLengthInterface, MatchRegularInterface, PlaceholderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The expected maximum number of characters per line of text for the UA to show. |
23
|
|
|
* |
24
|
|
|
* @param int $value Positive integer. |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
* |
28
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.cols |
29
|
|
|
*/ |
30
|
3 |
|
public function cols(int $value): self |
31
|
|
|
{ |
32
|
3 |
|
$new = clone $this; |
33
|
3 |
|
$new->attributes['cols'] = $value; |
34
|
3 |
|
return $new; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Enables submission of a value for the directionality of the element, and gives the name of the field that |
39
|
|
|
* contains that value. |
40
|
|
|
* |
41
|
|
|
* @param string $value Any string that is not empty. |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
* |
45
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.dirname |
46
|
|
|
*/ |
47
|
5 |
|
public function dirname(string $value): self |
48
|
|
|
{ |
49
|
5 |
|
if (empty($value)) { |
50
|
2 |
|
throw new InvalidArgumentException('The value cannot be empty.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
$new = clone $this; |
54
|
3 |
|
$new->attributes['dirname'] = $value; |
55
|
3 |
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
public function maxlength(int $value): self |
59
|
|
|
{ |
60
|
3 |
|
$new = clone $this; |
61
|
3 |
|
$new->attributes['maxlength'] = $value; |
62
|
3 |
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function minlength(int $value): self |
66
|
|
|
{ |
67
|
2 |
|
$new = clone $this; |
68
|
2 |
|
$new->attributes['minlength'] = $value; |
69
|
2 |
|
return $new; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function pattern(string $value): self |
73
|
|
|
{ |
74
|
|
|
$new = clone $this; |
75
|
|
|
$new->attributes['pattern'] = $value; |
76
|
|
|
return $new; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* It allows defining placeholder. |
81
|
|
|
* |
82
|
|
|
* @param string $value |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
* |
86
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.placeholder |
87
|
|
|
*/ |
88
|
2 |
|
public function placeholder(string $value): self |
89
|
|
|
{ |
90
|
2 |
|
$new = clone $this; |
91
|
2 |
|
$new->attributes['placeholder'] = $value; |
92
|
2 |
|
return $new; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* A Boolean attribute which, if present, means this field cannot be edited by the user. |
97
|
|
|
* Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value |
98
|
|
|
* property. |
99
|
|
|
* |
100
|
|
|
* @param bool $value |
101
|
|
|
* |
102
|
|
|
* @return static |
103
|
|
|
* |
104
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.readonly |
105
|
|
|
*/ |
106
|
2 |
|
public function readonly(bool $value = true): self |
107
|
|
|
{ |
108
|
2 |
|
$new = clone $this; |
109
|
2 |
|
$new->attributes['readonly'] = $value; |
110
|
2 |
|
return $new; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The number of lines of text for the UA to show. |
115
|
|
|
* |
116
|
|
|
* @param int $value |
117
|
|
|
* |
118
|
|
|
* @return static |
119
|
|
|
* |
120
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.rows |
121
|
|
|
*/ |
122
|
3 |
|
public function rows(int $value): self |
123
|
|
|
{ |
124
|
3 |
|
$new = clone $this; |
125
|
3 |
|
$new->attributes['rows'] = $value; |
126
|
3 |
|
return $new; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $value Contains the hard and soft values. |
131
|
|
|
* `hard` Instructs the UA to insert line breaks into the submitted value of the textarea such that each line has no |
132
|
|
|
* more characters than the value specified by the cols attribute. |
133
|
|
|
* `soft` Instructs the UA to add no line breaks to the submitted value of the textarea. |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
* |
137
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.wrap.hard |
138
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.wrap.soft |
139
|
|
|
*/ |
140
|
5 |
|
public function wrap(string $value = 'hard'): self |
141
|
|
|
{ |
142
|
5 |
|
if (!in_array($value, ['hard', 'soft'])) { |
143
|
2 |
|
throw new InvalidArgumentException('Invalid wrap value. Valid values are: hard, soft.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
$new = clone $this; |
147
|
3 |
|
$new->attributes['wrap'] = $value; |
148
|
3 |
|
return $new; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string the generated textarea tag. |
153
|
|
|
*/ |
154
|
43 |
|
protected function run(): string |
155
|
|
|
{ |
156
|
43 |
|
$attributes = $this->build($this->attributes); |
157
|
|
|
|
158
|
|
|
/** @link https://html.spec.whatwg.org/multipage/input.html#attr-input-value */ |
159
|
43 |
|
$value = $attributes['value'] ?? $this->getAttributeValue(); |
160
|
43 |
|
unset($attributes['value']); |
161
|
|
|
|
162
|
43 |
|
if (!is_string($value) && null !== $value) { |
163
|
2 |
|
throw new InvalidArgumentException('TextArea widget must be a string or null value.'); |
164
|
|
|
} |
165
|
|
|
|
166
|
41 |
|
return TextAreaTag::tag()->attributes($attributes)->content((string)$value)->render(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|