-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfFormField.class.php
319 lines (277 loc) · 8.96 KB
/
sfFormField.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfFormField represents a widget bind to a name and a value.
*
* @author Fabien Potencier <[email protected]>
*/
class sfFormField
{
protected static $toStringException;
/** @var sfWidgetForm */
protected $widget;
/** @var sfFormField|null */
protected $parent;
/** @var string */
protected $name = '';
/** @var string */
protected $value;
/** @var sfValidatorError|sfValidatorErrorSchema|null */
protected $error;
/**
* Constructor.
*
* @param sfWidgetForm $widget A sfWidget instance
* @param sfFormField $parent The sfFormField parent instance (null for the root widget)
* @param string $name The field name
* @param string $value The field value
* @param sfValidatorError $error A sfValidatorError instance
*/
public function __construct(sfWidgetForm $widget, ?sfFormField $parent = null, $name, $value, ?sfValidatorError $error = null)
{
$this->widget = $widget;
$this->parent = $parent;
$this->name = $name;
$this->value = $value;
$this->error = $error;
}
/**
* Returns the string representation of this form field.
*
* @return string The rendered field
*/
public function __toString()
{
try {
return $this->render();
} catch (Exception $e) {
self::setToStringException($e);
// we return a simple Exception message in case the form framework is used out of symfony.
return 'Exception: '.$e->getMessage();
}
}
/**
* Returns true if a form thrown an exception in the __toString() method.
*
* This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
*
* @return bool
*/
public static function hasToStringException()
{
return null !== self::$toStringException;
}
/**
* Gets the exception if one was thrown in the __toString() method.
*
* This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
*
* @return Exception
*/
public static function getToStringException()
{
return self::$toStringException;
}
/**
* Sets an exception thrown by the __toString() method.
*
* This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
*
* @param Exception $e The exception thrown by __toString()
*/
public static function setToStringException(Exception $e)
{
if (null === self::$toStringException) {
self::$toStringException = $e;
}
}
/**
* Renders the form field.
*
* @param array $attributes An array of HTML attributes
*
* @return string The rendered widget
*/
public function render($attributes = [])
{
if ($this->parent) {
return $this->parent->getWidget()->renderField($this->name, $this->value, $attributes, $this->error);
}
return $this->widget->render($this->name, $this->value, $attributes, $this->error);
}
/**
* Returns a formatted row.
*
* The formatted row will use the parent widget schema formatter.
* The formatted row contains the label, the field, the error and
* the help message.
*
* @param array $attributes An array of HTML attributes to merge with the current attributes
* @param string $label The label name (not null to override the current value)
* @param string $help The help text (not null to override the current value)
*
* @return string The formatted row
*/
public function renderRow($attributes = [], $label = null, $help = null)
{
if (null === $this->parent) {
throw new LogicException(sprintf('Unable to render the row for "%s".', $this->name));
}
$field = $this->parent->getWidget()->renderField($this->name, $this->value, !is_array($attributes) ? [] : $attributes, $this->error);
$error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error;
$help = null === $help ? $this->parent->getWidget()->getHelp($this->name) : $help;
return strtr($this->parent->getWidget()->getFormFormatter()->formatRow($this->renderLabel($label), $field, $error, $help), ['%hidden_fields%' => '']);
}
/**
* Returns a formatted error list.
*
* The formatted list will use the parent widget schema formatter.
*
* @return string The formatted error list
*/
public function renderError()
{
if (null === $this->parent) {
throw new LogicException(sprintf('Unable to render the error for "%s".', $this->name));
}
$error = $this->getWidget() instanceof sfWidgetFormSchema ? $this->getWidget()->getGlobalErrors($this->error) : $this->error;
return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error);
}
/**
* Returns the help text.
*
* @return string The help text
*/
public function renderHelp()
{
if (null === $this->parent) {
throw new LogicException(sprintf('Unable to render the help for "%s".', $this->name));
}
return $this->parent->getWidget()->getFormFormatter()->formatHelp($this->parent->getWidget()->getHelp($this->name));
}
/**
* Returns the label tag.
*
* @param string $label The label name (not null to override the current value)
* @param array $attributes Optional html attributes
*
* @return string The label tag
*/
public function renderLabel($label = null, $attributes = [])
{
if (null === $this->parent) {
throw new LogicException(sprintf('Unable to render the label for "%s".', $this->name));
}
if (null !== $label) {
$currentLabel = $this->parent->getWidget()->getLabel($this->name);
$this->parent->getWidget()->setLabel($this->name, $label);
}
$html = $this->parent->getWidget()->getFormFormatter()->generateLabel($this->name, $attributes);
if (null !== $label) {
$this->parent->getWidget()->setLabel($this->name, $currentLabel);
}
return $html;
}
/**
* Returns the label name given a widget name.
*
* @return string The label name
*/
public function renderLabelName()
{
if (null === $this->parent) {
throw new LogicException(sprintf('Unable to render the label name for "%s".', $this->name));
}
return $this->parent->getWidget()->getFormFormatter()->generateLabelName($this->name);
}
/**
* Returns the name attribute of the widget.
*
* @return string The name attribute of the widget
*/
public function renderName()
{
return $this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name;
}
/**
* Returns the id attribute of the widget.
*
* @return string The id attribute of the widget
*/
public function renderId()
{
return $this->widget->generateId($this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name, $this->value);
}
/**
* Returns true if the widget is hidden.
*
* @return bool true if the widget is hidden, false otherwise
*/
public function isHidden()
{
return $this->widget->isHidden();
}
/**
* Returns the widget name.
*
* @return mixed The widget name
*/
public function getName()
{
return $this->name;
}
/**
* Returns the widget value.
*
* @return mixed The widget value
*/
public function getValue()
{
return $this->value;
}
/**
* Returns the wrapped widget.
*
* @return sfWidget|sfWidgetFormSchemaDecorator A sfWidget instance
*/
public function getWidget()
{
return $this->widget;
}
/**
* Returns the parent form field.
*
* @return sfFormField A sfFormField instance
*/
public function getParent()
{
return $this->parent;
}
/**
* Returns the error for this field.
*
* @return sfValidatorError A sfValidatorError instance
*/
public function getError()
{
return $this->error;
}
/**
* Returns true is the field has an error.
*
* @return bool true if the field has some errors, false otherwise
*/
public function hasError()
{
if ($this->error instanceof sfValidatorErrorSchema) {
return $this->error->count() > 0;
}
return null !== $this->error;
}
}