Changeset 7045
- Timestamp:
- 01/14/08 13:08:18 (8 months ago)
- Files:
-
- branches/1.1/lib/form/sfFormField.class.php (modified) (4 diffs)
- branches/1.1/test/unit/form/sfFormFieldTest.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/form/sfFormField.class.php
r6196 r7045 80 80 public function renderRow($help = '') 81 81 { 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)); 85 85 } 86 86 87 87 $field = $this->parent->getWidget()->renderField($this->name, $this->value, $this->error); 88 88 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%' => '')); 90 92 } 91 93 … … 101 103 public function renderError() 102 104 { 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); 109 113 } 110 114 … … 116 120 public function renderLabel() 117 121 { 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)); 121 125 } 122 126 … … 131 135 public function renderLabelName() 132 136 { 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)); 136 140 } 137 141 branches/1.1/test/unit/form/sfFormFieldTest.php
r6197 r7045 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 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 )); 16 25 $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); 23 36 $f = $parent['title']; 37 $child = $parent['author']; 24 38 25 39 // ArrayAccess interface … … 70 84 // ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() 71 85 $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'); 73 87 $t->is($f->getValue(), 'symfony', '->getValue() returns the form field value'); 74 88 $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'); 76 90 $t->is($f->hasError(), true, '->hasError() returns true if the form field has some error'); 77 91 78 92 $errorSchema1 = new sfValidatorErrorSchema(new sfValidatorString()); 79 $errorSchema1->addError( $error =new sfValidatorError(new sfValidatorString(), 'error'), 'title1');93 $errorSchema1->addError(new sfValidatorError(new sfValidatorString(), 'error'), 'title1'); 80 94 $parent1 = new sfFormField($schema, null, 'article', array('title' => 'symfony'), $errorSchema1); 81 95 $f1 = $parent1['title']; … … 96 110 <th><label for="article_title">Title</label></th> 97 111 <td> <ul class="error_list"> 98 <li> error</li>112 <li>title error</li> 99 113 </ul> 100 114 <input type="text" name="article[title]" value="symfony" id="article_title" /></td> … … 107 121 <th><label for="article_title">Title</label></th> 108 122 <td> <ul class="error_list"> 109 <li> error</li>123 <li>title error</li> 110 124 </ul> 111 125 <input type="text" name="article[title]" value="symfony" id="article_title" /><br />help</td> … … 114 128 EOF; 115 129 $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 143 EOF; 144 $t->is($child->renderRow(), $output, '->renderRow() renders a row when the widget has a parent'); 116 145 try 117 146 { 118 147 $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 } 150 catch (LogicException $e) 151 { 152 $t->pass('->renderRow() throws an LogicException if the form field has no parent'); 124 153 } 125 154 … … 128 157 $output = <<<EOF 129 158 <ul class="error_list"> 130 <li> error</li>159 <li>title error</li> 131 160 </ul> 132 161 133 162 EOF; 134 163 $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 170 EOF; 171 $t->is($child['name']->renderError(), $output, '->renderRow() renders errors as HTML when the widget has a parent'); 172 135 173 try 136 174 { 137 175 $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 } 178 catch (LogicException $e) 179 { 180 $t->pass('->renderError() throws an LogicException if the form field has no parent'); 143 181 } 144 182 … … 149 187 { 150 188 $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 } 191 catch (LogicException $e) 192 { 193 $t->pass('->renderLabel() throws an LogicException if the form field has no parent'); 156 194 } 157 195 … … 162 200 { 163 201 $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 } 204 catch (LogicException $e) 205 { 206 $t->pass('->renderLabelName() throws an LogicException if the form field has no parent'); 169 207 } 170 208