Changeset 7152
- Timestamp:
- 01/22/08 15:19:03 (10 months ago)
- Files:
-
- branches/1.1/lib/form/sfForm.class.php (modified) (1 diff)
- branches/1.1/lib/form/sfFormField.class.php (modified) (1 diff)
- branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelCrud/default/template/templates/editSuccess.php (modified) (1 diff)
- branches/1.1/lib/widget/sfWidgetFormSchema.class.php (modified) (3 diffs)
- branches/1.1/test/unit/form/sfFormFieldTest.php (modified) (4 diffs)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/form/sfForm.class.php
r7124 r7152 107 107 108 108 /** 109 * Renders global errors associated with this form. 110 * 111 * @return string The rendered global errors 112 */ 113 public function renderGlobalErrors() 114 { 115 return $this->widgetSchema->getFormFormatter()->formatErrorRow($this->widgetSchema->getGlobalErrors($this->getErrorSchema())); 116 } 117 118 /** 109 119 * Binds the form with input values. 110 120 * branches/1.1/lib/form/sfFormField.class.php
r7054 r7152 105 105 } 106 106 107 $error = $this->error instanceof sfValidatorErrorSchema ? $this-> error->getGlobalErrors() : $this->error;107 $error = $this->error instanceof sfValidatorErrorSchema ? $this->getWidget()->getGlobalErrors($this->error) : $this->error; 108 108 109 109 return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error); branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelCrud/default/template/templates/editSuccess.php
r7120 r7152 20 20 [?php echo $form ?] 21 21 <?php else: ?> 22 22 [?php echo $form->renderGlobalErrors() ?] 23 23 <?php foreach ($form->getWidgetSchema()->getPositions() as $i => $name): ?> 24 24 <?php if ($form[$name]->isHidden()) continue ?> branches/1.1/lib/widget/sfWidgetFormSchema.class.php
r7053 r7152 325 325 $errorRows = array(); 326 326 327 // global errors 327 // render each field 328 foreach ($this->positions as $name) 329 { 330 $widget = $this[$name]; 331 $value = isset($values[$name]) ? $values[$name] : null; 332 333 if ($widget instanceof sfWidgetForm && $widget->isHidden()) 334 { 335 $hiddenRows[] = $widget->render($this->generateName($name), $value); 336 } 337 else 338 { 339 $error = isset($errors[$name]) ? $errors[$name] : array(); 340 $field = $this->renderField($name, $value, $error); 341 342 // don't add a label tag and errors if we embed a form schema 343 $label = $widget instanceof sfWidgetFormSchema ? $this->generateLabelName($name) : $this->generateLabel($name); 344 $error = $widget instanceof sfWidgetFormSchema ? array() : $error; 345 346 $rows[] = $formFormat->formatRow($label, $field, $error, $this->getHelp($name)); 347 } 348 } 349 350 // insert hidden fields in the last row 351 for ($i = 0, $max = count($rows); $i < $max; $i++) 352 { 353 $rows[$i] = strtr($rows[$i], array('%hidden_fields%' => $i == $max - 1 ? implode("\n", $hiddenRows) : '')); 354 } 355 356 return $this->getFormFormatter()->formatErrorRow($this->getGlobalErrors($errors)).implode('', $rows); 357 } 358 359 /** 360 * Gets errors that need to be included in global errors. 361 * 362 * @param array An array of errors 363 * 364 * @return string A HTML representation of global errors for the widget 365 */ 366 public function getGlobalErrors($errors) 367 { 328 368 $globalErrors = array(); 369 370 // global errors and errors for non existent fields 329 371 if (!is_null($errors)) 330 372 { … … 338 380 } 339 381 340 // render each field382 // errors for hidden fields 341 383 foreach ($this->positions as $name) 342 384 { 343 $widget = $this[$name]; 344 $value = isset($values[$name]) ? $values[$name] : null; 345 346 if ($widget instanceof sfWidgetForm && $widget->isHidden()) 347 { 348 $hiddenRows[] = $widget->render($this->generateName($name), $value); 385 if ($this[$name] instanceof sfWidgetForm && $this[$name]->isHidden()) 386 { 349 387 if (isset($errors[$name])) 350 388 { … … 352 390 } 353 391 } 354 else 355 { 356 $error = isset($errors[$name]) ? $errors[$name] : array(); 357 $field = $this->renderField($name, $value, $error); 358 359 // don't add a label tag and errors if we embed a form schema 360 $label = $widget instanceof sfWidgetFormSchema ? $this->generateLabelName($name) : $this->generateLabel($name); 361 $error = $widget instanceof sfWidgetFormSchema ? array() : $error; 362 363 $rows[] = $formFormat->formatRow($label, $field, $error, $this->getHelp($name)); 364 } 365 } 366 367 // insert hidden fields in the last row 368 for ($i = 0, $max = count($rows); $i < $max; $i++) 369 { 370 $rows[$i] = strtr($rows[$i], array('%hidden_fields%' => $i == $max - 1 ? implode("\n", $hiddenRows) : '')); 371 } 372 373 return $formFormat->formatErrorRow($globalErrors).implode('', $rows); 392 } 393 394 return $globalErrors; 374 395 } 375 396 branches/1.1/test/unit/form/sfFormFieldTest.php
r7053 r7152 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(2 1, new lime_output_color());13 $t = new lime_test(22, new lime_output_color()); 14 14 15 15 // widgets 16 16 $authorSchema = new sfWidgetFormSchema(array( 17 'id' => new sfWidgetFormInputHidden(), 17 18 'name' => $nameWidget = new sfWidgetFormInput(), 18 19 )); … … 91 92 <li>name error</li> 92 93 </ul> 93 <input type="text" name="article[author][name]" value="Fabien" id="article_author_name" />< /td>94 <input type="text" name="article[author][name]" value="Fabien" id="article_author_name" /><input type="hidden" name="article[author][id]" id="article_author_id" /></td> 94 95 </tr> 95 96 </td> … … 106 107 { 107 108 $t->pass('->renderRow() throws an LogicException if the form field has no parent'); 108 }109 110 // ->renderError();111 $t->diag('->renderError()');112 $output = <<<EOF113 <ul class="error_list">114 <li>title error</li>115 </ul>116 117 EOF;118 $t->is($f->renderError(), $output, '->renderError() renders errors as HTML');119 $t->is($child->renderError(), '', '->renderRow() renders errors as HTML when the widget has a parent');120 $output = <<<EOF121 <ul class="error_list">122 <li>name error</li>123 </ul>124 125 EOF;126 $t->is($child['name']->renderError(), $output, '->renderRow() renders errors as HTML when the widget has a parent');127 128 try129 {130 $parent->renderError();131 $t->fail('->renderError() throws an LogicException if the form field has no parent');132 }133 catch (LogicException $e)134 {135 $t->pass('->renderError() throws an LogicException if the form field has no parent');136 109 } 137 110 … … 165 138 $t->diag('->isHidden()'); 166 139 $t->is($f->isHidden(), false, '->isHidden() is a proxy method to the isHidden() method of the widget'); 140 141 // ->renderError(); 142 $t->diag('->renderError()'); 143 $output = <<<EOF 144 <ul class="error_list"> 145 <li>title error</li> 146 </ul> 147 148 EOF; 149 $t->is($f->renderError(), $output, '->renderError() renders errors as HTML'); 150 $t->is($child->renderError(), '', '->renderRow() renders errors as HTML when the widget has a parent'); 151 $output = <<<EOF 152 <ul class="error_list"> 153 <li>name error</li> 154 </ul> 155 156 EOF; 157 $t->is($child['name']->renderError(), $output, '->renderRow() renders errors as HTML when the widget has a parent'); 158 159 try 160 { 161 $parent->renderError(); 162 $t->fail('->renderError() throws an LogicException if the form field has no parent'); 163 } 164 catch (LogicException $e) 165 { 166 $t->pass('->renderError() throws an LogicException if the form field has no parent'); 167 } 168 169 // global errors 170 $authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); 171 $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); 172 $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'non existent field error'), 'non_existent_field'); 173 $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'hidden field error'), 'id'); 174 175 $articleErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); 176 $articleErrorSchema->addError($titleError = new sfValidatorError(new sfValidatorString(), 'title error'), 'title'); 177 $articleErrorSchema->addError($authorErrorSchema, 'author'); 178 179 $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); 180 $child = $parent['author']; 181 $output = <<<EOF 182 <ul class="error_list"> 183 <li>non existent field error</li> 184 <li>Id: hidden field error</li> 185 </ul> 186 187 EOF; 188 $t->is($child->renderError(), $output, '->renderError() renders global errors as expected (global errors, hidden field errors, non existent field errors)'); 189 branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php
r7053 r7152 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 59, new lime_output_color());13 $t = new lime_test(60, new lime_output_color()); 14 14 15 15 $w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); … … 254 254 $t->pass('->moveField() throws an LogicException if you don\'t pass a relative field name with BEFORE'); 255 255 } 256 257 // ->getGlobalErrors() 258 $t->diag('->getGlobalErrors()'); 259 $w = new sfWidgetFormSchema(); 260 $w['w1'] = $w1; 261 $w['w2'] = new sfWidgetFormInputHidden(); 262 $w['w3'] = new sfWidgetFormSchema(); 263 $w['w3']['w1'] = $w1; 264 $w['w3']['w2'] = new sfWidgetFormInputHidden(); 265 $errors = array( 266 'global error', 267 'w1' => 'error for w1', 268 'w2' => 'error for w2', 269 'w4' => array( 270 'w1' => 'error for w4/w1', 271 'w2' => 'error for w4/w2', 272 'w3' => 'error for w4/w3', 273 ), 274 'w4' => 'error for w4', 275 ); 276 $t->is($w->getGlobalErrors($errors), array('global error', 'error for w4', 'W2' => 'error for w2'), '->getGlobalErrors() returns an array of global errors, errors for hidden fields, and errors for non existent fields'); 256 277 257 278 // ->render()