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