Development

Changeset 7045

You must first sign up to be able to contribute.

Changeset 7045

Show
Ignore:
Timestamp:
01/14/08 13:08:18 (8 months ago)
Author:
fabien
Message:

fixed sfFormField::render*() methods for widgets with parents

Files:

Legend:

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

    r6196 r7045  
    8080  public function renderRow($help = '') 
    8181  { 
    82     if ($this->widget instanceof sfWidgetFormSchema
    83     { 
    84       throw new LogicException('Unable to format a row on a sfWidgetFormSchema.'); 
     82    if (is_null($this->parent)
     83    { 
     84      throw new LogicException(sprintf('Unable to render the row for "%s".', $this->name)); 
    8585    } 
    8686 
    8787    $field = $this->parent->getWidget()->renderField($this->name, $this->value, $this->error); 
    8888 
    89     return strtr($this->parent->getWidget()->getFormFormatter()->formatRow($this->renderLabel(), $field, $this->error, $help), array('%hidden_fields%' => '')); 
     89    $error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error; 
     90 
     91    return strtr($this->parent->getWidget()->getFormFormatter()->formatRow($this->renderLabel(), $field, $error, $help), array('%hidden_fields%' => '')); 
    9092  } 
    9193 
     
    101103  public function renderError() 
    102104  { 
    103     if ($this->widget instanceof sfWidgetFormSchema) 
    104     { 
    105       throw new LogicException('Unable to format an error list on a sfWidgetFormSchema.'); 
    106     } 
    107  
    108     return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($this->error); 
     105    if (is_null($this->parent)) 
     106    { 
     107      throw new LogicException(sprintf('Unable to render the error for "%s".', $this->name)); 
     108    } 
     109 
     110    $error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error; 
     111 
     112    return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error); 
    109113  } 
    110114 
     
    116120  public function renderLabel() 
    117121  { 
    118     if ($this->widget instanceof sfWidgetFormSchema
    119     { 
    120       throw new LogicException('Unable to render a label on a sfWidgetFormSchema.'); 
     122    if (is_null($this->parent)
     123    { 
     124      throw new LogicException(sprintf('Unable to render the label for "%s".', $this->name)); 
    121125    } 
    122126 
     
    131135  public function renderLabelName() 
    132136  { 
    133     if ($this->widget instanceof sfWidgetFormSchema
    134     { 
    135       throw new LogicException('Unable to render a label name on a sfWidgetFormSchema.'); 
     137    if (is_null($this->parent)
     138    { 
     139      throw new LogicException(sprintf('Unable to render the label name for "%s".', $this->name)); 
    136140    } 
    137141 
  • branches/1.1/test/unit/form/sfFormFieldTest.php

    r6197 r7045  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(25, new lime_output_color()); 
    14  
    15 $schema = new sfWidgetFormSchema(); 
     13$t = new lime_test(28, new lime_output_color()); 
     14 
     15// widgets 
     16$authorSchema = new sfWidgetFormSchema(array( 
     17  'name' => $nameWidget = new sfWidgetFormInput(), 
     18)); 
     19$authorSchema->setNameFormat('article[author][%s]'); 
     20 
     21$schema = new sfWidgetFormSchema(array( 
     22  'title'  => $titleWidget = new sfWidgetFormInput(), 
     23  'author' => $authorSchema, 
     24)); 
    1625$schema->setNameFormat('article[%s]'); 
    17 $widget = new sfWidgetFormInput(); 
    18 $schema['title'] = $widget; 
    19  
    20 $errorSchema = new sfValidatorErrorSchema(new sfValidatorString()); 
    21 $errorSchema->addError($error = new sfValidatorError(new sfValidatorString(), 'error'), 'title'); 
    22 $parent = new sfFormField($schema, null, 'article', array('title' => 'symfony'), $errorSchema); 
     26 
     27// errors 
     28$authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); 
     29$authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); 
     30 
     31$articleErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); 
     32$articleErrorSchema->addError($titleError = new sfValidatorError(new sfValidatorString(), 'title error'), 'title'); 
     33$articleErrorSchema->addError($authorErrorSchema, 'author'); 
     34 
     35$parent = new sfFormField($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); 
    2336$f = $parent['title']; 
     37$child = $parent['author']; 
    2438 
    2539// ArrayAccess interface 
     
    7084// ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() 
    7185$t->diag('->getValue() ->getWidget() ->getParent() ->getError() ->hasError()'); 
    72 $t->is($f->getWidget(), $widget, '->getWidget() returns the form field widget'); 
     86$t->is($f->getWidget(), $titleWidget, '->getWidget() returns the form field widget'); 
    7387$t->is($f->getValue(), 'symfony', '->getValue() returns the form field value'); 
    7488$t->is($f->getParent(), $parent, '->getParent() returns the form field parent'); 
    75 $t->is($f->getError(), $error, '->getError() returns the form field error'); 
     89$t->is($f->getError(), $titleError, '->getError() returns the form field error'); 
    7690$t->is($f->hasError(), true, '->hasError() returns true if the form field has some error'); 
    7791 
    7892$errorSchema1 = new sfValidatorErrorSchema(new sfValidatorString()); 
    79 $errorSchema1->addError($error = new sfValidatorError(new sfValidatorString(), 'error'), 'title1'); 
     93$errorSchema1->addError(new sfValidatorError(new sfValidatorString(), 'error'), 'title1'); 
    8094$parent1 = new sfFormField($schema, null, 'article', array('title' => 'symfony'), $errorSchema1); 
    8195$f1 = $parent1['title']; 
     
    96110  <th><label for="article_title">Title</label></th> 
    97111  <td>  <ul class="error_list"> 
    98     <li>error</li> 
     112    <li>title error</li> 
    99113  </ul> 
    100114<input type="text" name="article[title]" value="symfony" id="article_title" /></td> 
     
    107121  <th><label for="article_title">Title</label></th> 
    108122  <td>  <ul class="error_list"> 
    109     <li>error</li> 
     123    <li>title error</li> 
    110124  </ul> 
    111125<input type="text" name="article[title]" value="symfony" id="article_title" /><br />help</td> 
     
    114128EOF; 
    115129$t->is($f->renderRow('help'), $output, '->renderRow() can take a help message'); 
     130$output = <<<EOF 
     131<tr> 
     132  <th><label for="article_author">Author</label></th> 
     133  <td><tr> 
     134  <th><label for="article_author_name">Name</label></th> 
     135  <td>  <ul class="error_list"> 
     136    <li>name error</li> 
     137  </ul> 
     138<input type="text" name="article[author][name]" value="Fabien" id="article_author_name" /></td> 
     139</tr> 
     140</td> 
     141</tr> 
     142 
     143EOF; 
     144$t->is($child->renderRow(), $output, '->renderRow() renders a row when the widget has a parent'); 
    116145try 
    117146{ 
    118147  $parent->renderRow(); 
    119   $t->fail('->renderRow() throws an LogicException if the form field is a schema'); 
    120 } 
    121 catch (LogicException $e) 
    122 { 
    123   $t->pass('->renderRow() throws an LogicException if the form field is a schema'); 
     148  $t->fail('->renderRow() throws an LogicException if the form field has no parent'); 
     149} 
     150catch (LogicException $e) 
     151{ 
     152  $t->pass('->renderRow() throws an LogicException if the form field has no parent'); 
    124153} 
    125154 
     
    128157$output = <<<EOF 
    129158  <ul class="error_list"> 
    130     <li>error</li> 
     159    <li>title error</li> 
    131160  </ul> 
    132161 
    133162EOF; 
    134163$t->is($f->renderError(), $output, '->renderError() renders errors as HTML'); 
     164$t->is($child->renderError(), '', '->renderRow() renders errors as HTML when the widget has a parent'); 
     165$output = <<<EOF 
     166  <ul class="error_list"> 
     167    <li>name error</li> 
     168  </ul> 
     169 
     170EOF; 
     171$t->is($child['name']->renderError(), $output, '->renderRow() renders errors as HTML when the widget has a parent'); 
     172 
    135173try 
    136174{ 
    137175  $parent->renderError(); 
    138   $t->fail('->renderError() throws an LogicException if the form field is a schema'); 
    139 } 
    140 catch (LogicException $e) 
    141 { 
    142   $t->pass('->renderError() throws an LogicException if the form field is a schema'); 
     176  $t->fail('->renderError() throws an LogicException if the form field has no parent'); 
     177} 
     178catch (LogicException $e) 
     179{ 
     180  $t->pass('->renderError() throws an LogicException if the form field has no parent'); 
    143181} 
    144182 
     
    149187{ 
    150188  $parent->renderLabel(); 
    151   $t->fail('->renderLabel() throws an LogicException if the form field is a schema'); 
    152 } 
    153 catch (LogicException $e) 
    154 { 
    155   $t->pass('->renderLabel() throws an LogicException if the form field is a schema'); 
     189  $t->fail('->renderLabel() throws an LogicException if the form field has no parent'); 
     190} 
     191catch (LogicException $e) 
     192{ 
     193  $t->pass('->renderLabel() throws an LogicException if the form field has no parent'); 
    156194} 
    157195 
     
    162200{ 
    163201  $parent->renderLabelName(); 
    164   $t->fail('->renderLabelName() throws an LogicException if the form field is a schema'); 
    165 } 
    166 catch (LogicException $e) 
    167 { 
    168   $t->pass('->renderLabelName() throws an LogicException if the form field is a schema'); 
     202  $t->fail('->renderLabelName() throws an LogicException if the form field has no parent'); 
     203} 
     204catch (LogicException $e) 
     205{ 
     206  $t->pass('->renderLabelName() throws an LogicException if the form field has no parent'); 
    169207} 
    170208