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