Development

Changeset 7152

You must first sign up to be able to contribute.

Changeset 7152

Show
Ignore:
Timestamp:
01/22/08 15:19:03 (10 months ago)
Author:
fabien
Message:

fixed form global errors management

  • added a sfWidgetFormSchema::getGlobalErrors() method
  • added a sfForm::renderGlobalErrors() method
  • fixed sfFormField::renderError() to include all global errors
  • fixed CRUD editSuccess.php template
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/form/sfForm.class.php

    r7124 r7152  
    107107 
    108108  /** 
     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  /** 
    109119   * Binds the form with input values. 
    110120   * 
  • branches/1.1/lib/form/sfFormField.class.php

    r7054 r7152  
    105105    } 
    106106 
    107     $error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error; 
     107    $error = $this->error instanceof sfValidatorErrorSchema ? $this->getWidget()->getGlobalErrors($this->error) : $this->error; 
    108108 
    109109    return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error); 
  • branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelCrud/default/template/templates/editSuccess.php

    r7120 r7152  
    2020      [?php echo $form ?] 
    2121<?php else: ?> 
    22  
     22      [?php echo $form->renderGlobalErrors() ?] 
    2323<?php foreach ($form->getWidgetSchema()->getPositions() as $i => $name): ?> 
    2424<?php if ($form[$name]->isHidden()) continue ?> 
  • branches/1.1/lib/widget/sfWidgetFormSchema.class.php

    r7053 r7152  
    325325    $errorRows = array(); 
    326326 
    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  { 
    328368    $globalErrors = array(); 
     369 
     370    // global errors and errors for non existent fields 
    329371    if (!is_null($errors)) 
    330372    { 
     
    338380    } 
    339381 
    340     // render each field 
     382    // errors for hidden fields 
    341383    foreach ($this->positions as $name) 
    342384    { 
    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      { 
    349387        if (isset($errors[$name])) 
    350388        { 
     
    352390        } 
    353391      } 
    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; 
    374395  } 
    375396 
  • branches/1.1/test/unit/form/sfFormFieldTest.php

    r7053 r7152  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(21, new lime_output_color()); 
     13$t = new lime_test(22, new lime_output_color()); 
    1414 
    1515// widgets 
    1616$authorSchema = new sfWidgetFormSchema(array( 
     17  'id'   => new sfWidgetFormInputHidden(), 
    1718  'name' => $nameWidget = new sfWidgetFormInput(), 
    1819)); 
     
    9192    <li>name error</li> 
    9293  </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> 
    9495</tr> 
    9596</td> 
     
    106107{ 
    107108  $t->pass('->renderRow() throws an LogicException if the form field has no parent'); 
    108 } 
    109  
    110 // ->renderError(); 
    111 $t->diag('->renderError()'); 
    112 $output = <<<EOF 
    113   <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 = <<<EOF 
    121   <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 try 
    129 { 
    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'); 
    136109} 
    137110 
     
    165138$t->diag('->isHidden()'); 
    166139$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 
     148EOF; 
     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 
     156EOF; 
     157$t->is($child['name']->renderError(), $output, '->renderRow() renders errors as HTML when the widget has a parent'); 
     158 
     159try 
     160{ 
     161  $parent->renderError(); 
     162  $t->fail('->renderError() throws an LogicException if the form field has no parent'); 
     163} 
     164catch (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 
     187EOF; 
     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  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(59, new lime_output_color()); 
     13$t = new lime_test(60, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 
     
    254254  $t->pass('->moveField() throws an LogicException if you don\'t pass a relative field name with BEFORE'); 
    255255} 
     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'); 
    256277 
    257278// ->render()