Ticket #4567: patch.diff
| File patch.diff, 7.1 kB (added by pablodip, 3 months ago) |
|---|
-
test/unit/widget/sfWidgetFormSchemaTest.php
old new 10 10 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 61, new lime_output_color());13 $t = new lime_test(70, new lime_output_color()); 14 14 15 15 $w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 16 16 $w2 = new sfWidgetFormInput(); … … 358 358 $t->ok($widget == $f[$name], '__clone() clones embedded widgets'); 359 359 } 360 360 361 $w = new sfWidgetFormSchema(); 362 $w->addFormFormatter('table', new sfWidgetFormSchemaFormatterTable($w)); 363 $w->addFormFormatter('list', new sfWidgetFormSchemaFormatterList($w)); 364 $w1 = clone $w; 365 $f1 = $w1->getFormFormatters(); 366 $f = $w->getFormFormatters(); 367 $t->is(array_keys($f1), array_keys($f), '__clone() clones form formatters'); 368 foreach ($f1 as $key => $formFormatter) 369 { 370 $t->ok($formFormatter !== $f[$key], '__clone() clones form formatters'); 371 $t->is(get_class($formFormatter), get_class($f[$key]), '__clone() clones form formatters'); 372 373 $t->ok($formFormatter->getWidgetSchema() !== $f[$key]->getWidgetSchema(), '__clone() clones form formatters'); 374 $t->is(get_class($formFormatter->getWidgetSchema()), get_class($f[$key]->getWidgetSchema()), '__clone() clones form formatters'); 375 } 376 361 377 // setDefaultFormFormatterName() 362 378 $t->diag('setDefaultFormFormatterName()'); 363 379 $w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); -
lib/widget/sfWidgetFormSchemaFormatter.class.php
old new 18 18 */ 19 19 abstract class sfWidgetFormSchemaFormatter 20 20 { 21 protected static 21 protected static 22 22 $translationCallable = null; 23 23 24 24 protected 25 25 $rowFormat = '', 26 26 $helpFormat = '%help%', … … 39 39 */ 40 40 public function __construct(sfWidgetFormSchema $widgetSchema) 41 41 { 42 $this->setWidgetSchema($widgetSchema); 43 } 44 45 public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 46 { 42 47 $this->widgetSchema = $widgetSchema; 43 48 } 44 49 50 public function getWidgetSchema() 51 { 52 return $this->widgetSchema; 53 } 54 45 55 public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 46 56 { 47 57 return strtr($this->getRowFormat(), array( … … 52 62 '%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields%' : $hiddenFields, 53 63 )); 54 64 } 55 65 56 66 /** 57 67 * Translates a string using an i18n callable, if it has been provided 58 68 * … … 64 74 { 65 75 if (false === $subject) 66 76 { 67 return false; 77 return false; 68 78 } 69 79 70 80 if (is_null(self::$translationCallable)) … … 80 90 81 91 return strtr($subject, $parameters); 82 92 } 83 93 84 94 $catalogue = $this->getTranslationCatalogue(); 85 95 86 96 if (self::$translationCallable instanceof sfCallable) … … 100 110 { 101 111 return self::$translationCallable; 102 112 } 103 113 104 114 /** 105 * Sets a callable which aims to translate form labels, errors and help messages 115 * Sets a callable which aims to translate form labels, errors and help messages 106 116 * 107 117 * @param mixed $callable 108 * 118 * 109 119 * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 110 120 */ 111 121 static public function setTranslationCallable($callable) … … 114 124 { 115 125 throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 116 126 } 117 127 118 128 self::$translationCallable = $callable; 119 129 } 120 130 121 131 public function formatHelp($help) 122 132 { 123 133 if (!$help) … … 152 162 153 163 return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 154 164 } 155 165 156 166 /** 157 167 * Generates a label for the given field name. 158 168 * … … 169 179 { 170 180 return ''; 171 181 } 172 182 173 183 $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 174 184 $attributes = array_merge($attributes, array('for' => $widgetId)); 175 185 176 186 return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 177 187 } 178 188 … … 186 196 public function generateLabelName($name) 187 197 { 188 198 $label = $this->widgetSchema->getLabel($name); 189 199 190 200 if (!$label && false !== $label) 191 201 { 192 202 $label = str_replace('_', ' ', ucfirst($name)); … … 194 204 195 205 return $this->translate($label); 196 206 } 197 207 198 208 /** 199 209 * Get i18n catalogue name 200 210 * … … 204 214 { 205 215 return $this->translationCatalogue; 206 216 } 207 217 208 218 /** 209 219 * Set an i18n catalogue name 210 220 * … … 217 227 { 218 228 throw new InvalidArgumentException('Catalogue name must be a string'); 219 229 } 220 230 221 231 $this->translationCatalogue = $catalogue; 222 232 } 223 233 224 234 protected function unnestErrors($errors, $prefix = '') 225 235 { 226 236 $newErrors = array(); … … 255 265 256 266 return $newErrors; 257 267 } 258 268 259 269 public function setRowFormat($format) 260 270 { 261 271 $this->rowFormat = $format; -
lib/widget/sfWidgetFormSchema.class.php
old new 25 25 LAST = 'last', 26 26 BEFORE = 'before', 27 27 AFTER = 'after'; 28 28 29 29 protected static 30 30 $defaultFormatterName = 'table'; 31 31 … … 102 102 { 103 103 return $this->formFormatters; 104 104 } 105 105 106 106 /** 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 109 109 * do it in a project or application configuration class: 110 * 110 * 111 111 * <pre> 112 112 * class ProjectConfiguration extends sfProjectConfiguration 113 113 * { … … 116 116 * sfWidgetFormSchema::setDefaultFormFormatterName('list'); 117 117 * } 118 118 * } 119 * </pre> 119 * </pre> 120 120 * 121 121 * @param string $name New default formatter name 122 122 */ … … 159 159 if (!isset($this->formFormatters[$name])) 160 160 { 161 161 $class = 'sfWidgetFormSchemaFormatter'.ucfirst($name); 162 162 163 163 if (!class_exists($class)) 164 164 { 165 165 throw new InvalidArgumentException(sprintf('The form formatter "%s" does not exist.', $name)); 166 166 } 167 167 168 168 $this->formFormatters[$name] = new $class($this); 169 169 } 170 170 171 171 return $this->formFormatters[$name]; 172 172 } 173 173 … … 675 675 // offsetSet will clone the field and change the parent 676 676 $this[$name] = $field; 677 677 } 678 679 foreach ($this->formFormatters as &$formFormatter) 680 { 681 $formFormatter = clone $formFormatter; 682 $formFormatter->setWidgetSchema($this); 683 } 678 684 } 679 685 }