Development

/branches/1.1/lib/form/sfFormField.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/form/sfFormField.class.php

Revision 10439, 7.1 kB (checked in by nicolas, 6 years ago)

[1.1] fixed exception cannot be thrown in the sfFormField::__toString method

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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 !is_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 (is_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     return $this->widget->render($this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name, $this->value, $attributes, $this->error);
118   }
119
120   /**
121    * Returns a formatted row.
122    *
123    * The formatted row will use the parent widget schema formatter.
124    * The formatted row contains the label, the field, the error and
125    * the help message.
126    *
127    * @param  array  $attributes   An array of HTML attributes to merge with the current attributes
128    * @param  string $label        The label name (not null to override the current value)
129    * @param  string $help         The help text (not null to override the current value)
130    *
131    * @return string The formatted row
132    */
133   public function renderRow($attributes = array(), $label = null, $help = null)
134   {
135     if (is_null($this->parent))
136     {
137       throw new LogicException(sprintf('Unable to render the row for "%s".', $this->name));
138     }
139
140     $field = $this->parent->getWidget()->renderField($this->name, $this->value, !is_array($attributes) ? array() : $attributes, $this->error);
141
142     $error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error;
143
144     $help = is_null($help) ? $this->parent->getWidget()->getHelp($this->name) : $help;
145
146     return strtr($this->parent->getWidget()->getFormFormatter()->formatRow($this->renderLabel($label), $field, $error, $help), array('%hidden_fields%' => ''));
147   }
148
149   /**
150    * Returns a formatted error list.
151    *
152    * The formatted list will use the parent widget schema formatter.
153    *
154    * @return string The formatted error list
155    */
156   public function renderError()
157   {
158     if (is_null($this->parent))
159     {
160       throw new LogicException(sprintf('Unable to render the error for "%s".', $this->name));
161     }
162
163     $error = $this->getWidget() instanceof sfWidgetFormSchema ? $this->getWidget()->getGlobalErrors($this->error) : $this->error;
164
165     return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error);
166   }
167
168   /**
169    * Returns the label tag.
170    *
171    * @param  string $label       The label name (not null to override the current value)
172    * @param  array  $attributes  Optional html attributes
173    *
174    * @return string The label tag
175    */
176   public function renderLabel($label = null, $attributes = array())
177   {
178     if (is_null($this->parent))
179     {
180       throw new LogicException(sprintf('Unable to render the label for "%s".', $this->name));
181     }
182
183     if (!is_null($label))
184     {
185       $currentLabel = $this->parent->getWidget()->getLabel($this->name);
186       $this->parent->getWidget()->setLabel($this->name, $label);
187     }
188
189     $html = $this->parent->getWidget()->getFormFormatter()->generateLabel($this->name, $attributes);
190
191     if (!is_null($label))
192     {
193       $this->parent->getWidget()->setLabel($this->name, $currentLabel);
194     }
195
196     return $html;
197   }
198
199   /**
200    * Returns the label name given a widget name.
201    *
202    * @return string The label name
203    */
204   public function renderLabelName()
205   {
206     if (is_null($this->parent))
207     {
208       throw new LogicException(sprintf('Unable to render the label name for "%s".', $this->name));
209     }
210
211     return $this->parent->getWidget()->getFormFormatter()->generateLabelName($this->name);
212   }
213
214   /**
215    * Returns true if the widget is hidden.
216    *
217    * @return Boolean true if the widget is hidden, false otherwise
218    */
219   public function isHidden()
220   {
221     return $this->widget->isHidden();
222   }
223
224   /**
225    * Returns the widget value.
226    *
227    * @return mixed The widget value
228    */
229   public function getValue()
230   {
231     return $this->value;
232   }
233
234   /**
235    * Returns the wrapped widget.
236    *
237    * @return sfWidget A sfWidget instance
238    */
239   public function getWidget()
240   {
241     return $this->widget;
242   }
243
244   /**
245    * Returns the parent form field.
246    *
247    * @return sfFormField A sfFormField instance
248    */
249   public function getParent()
250   {
251     return $this->parent;
252   }
253
254   /**
255    * Returns the error for this field.
256    *
257    * @return sfValidatorError A sfValidatorError instance
258    */
259   public function getError()
260   {
261     return $this->error;
262   }
263
264   /**
265    * Returns true is the field has an error.
266    *
267    * @return Boolean true if the field has some errors, false otherwise
268    */
269   public function hasError()
270   {
271     return !is_null($this->error) && count($this->error);
272   }
273 }
274
Note: See TracBrowser for help on using the browser.