Changeset 7047
- Timestamp:
- 01/14/08 14:50:17 (1 year ago)
- Files:
-
- branches/1.1/lib/form/sfForm.class.php (modified) (3 diffs)
- branches/1.1/lib/form/sfFormField.class.php (modified) (4 diffs)
- branches/1.1/lib/form/sfFormFieldSchema.class.php (added)
- branches/1.1/test/unit/form/sfFormFieldSchemaTest.php (added)
- branches/1.1/test/unit/form/sfFormFieldTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/form/sfForm.class.php
r6990 r7047 573 573 $values = $this->isBound ? $this->taintedValues : $this->defaults; 574 574 575 $this->formFields[$name] = new sfFormField($widget, $this->getFormField(), $name, isset($values[$name]) ? $values[$name] : null, $this->errorSchema[$name]); 575 $class = $widget instanceof sfWidgetFormSchema ? 'sfFormFieldSchema' : 'sfFormField'; 576 577 $this->formFields[$name] = new $class($widget, $this->getFormField(), $name, isset($values[$name]) ? $values[$name] : null, $this->errorSchema[$name]); 576 578 } 577 579 … … 609 611 * Returns a form field for the main widget schema. 610 612 * 611 * @return sfFormField A sfFormFieldinstance613 * @return sfFormFieldSchema A sfFormFieldSchema instance 612 614 */ 613 615 protected function getFormField() … … 615 617 if (is_null($this->formField)) 616 618 { 617 $this->formField = new sfFormField ($this->widgetSchema, null, null, $this->isBound ? $this->taintedValues : $this->defaults, $this->errorSchema);619 $this->formField = new sfFormFieldSchema($this->widgetSchema, null, null, $this->isBound ? $this->taintedValues : $this->defaults, $this->errorSchema); 618 620 } 619 621 branches/1.1/lib/form/sfFormField.class.php
r7045 r7047 17 17 * @version SVN: $Id$ 18 18 */ 19 class sfFormField implements ArrayAccess19 class sfFormField 20 20 { 21 21 protected … … 24 24 $name = '', 25 25 $value = null, 26 $error = null, 27 $fields = array(); 26 $error = null; 28 27 29 28 /** 30 29 * Constructor. 31 30 * 32 * @param sfWidget A sfWidget instance31 * @param sfWidgetForm A sfWidget instance 33 32 * @param sfFormField The sfFormField parent instance (null for the root widget) 34 33 * @param string The field name … … 36 35 * @param sfValidatorError A sfValidatorError instance 37 36 */ 38 public function __construct(sfWidget $widget, sfFormField $parent = null, $name, $value, sfValidatorError $error = null)37 public function __construct(sfWidgetForm $widget, sfFormField $parent = null, $name, $value, sfValidatorError $error = null) 39 38 { 40 39 $this->widget = $widget; … … 202 201 return !is_null($this->error) && count($this->error); 203 202 } 204 205 /**206 * Returns true if the bound field exists (implements the ArrayAccess interface).207 *208 * @param string The name of the bound field209 *210 * @return Boolean true if the widget exists, false otherwise211 */212 public function offsetExists($name)213 {214 return $this->widget instanceof sfWidgetFormSchema ? isset($this->widget[$name]) : false;215 }216 217 /**218 * Returns the form field associated with the name (implements the ArrayAccess interface).219 *220 * @param string The offset of the value to get221 *222 * @return sfFormField A form field instance223 */224 public function offsetGet($name)225 {226 if (!isset($this->fields[$name]))227 {228 if (!$this->widget instanceof sfWidgetFormSchema)229 {230 throw new LogicException(sprintf('Cannot get a form field on a non widget schema (%s given).', get_class($this->widget)));231 }232 233 if (is_null($widget = $this->widget[$name]))234 {235 throw new InvalidArgumentException(sprintf('Widget "%s" does not exist.', $name));236 }237 238 $this->fields[$name] = new sfFormField($widget, $this, $name, isset($this->value[$name]) ? $this->value[$name] : null, isset($this->error[$name]) ? $this->error[$name] : null);239 }240 241 return $this->fields[$name];242 }243 244 /**245 * Throws an exception saying that values cannot be set (implements the ArrayAccess interface).246 *247 * @param string (ignored)248 * @param string (ignored)249 *250 * @throws <b>LogicException</b>251 */252 public function offsetSet($offset, $value)253 {254 throw new LogicException('Cannot update form fields (read-only).');255 }256 257 /**258 * Throws an exception saying that values cannot be unset (implements the ArrayAccess interface).259 *260 * @param string (ignored)261 *262 * @throws LogicException263 */264 public function offsetUnset($offset)265 {266 throw new LogicException('Cannot remove form fields (read-only).');267 }268 203 } branches/1.1/test/unit/form/sfFormFieldTest.php
r7045 r7047 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(2 8, new lime_output_color());13 $t = new lime_test(21, new lime_output_color()); 14 14 15 15 // widgets … … 33 33 $articleErrorSchema->addError($authorErrorSchema, 'author'); 34 34 35 $parent = new sfFormField ($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema);35 $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); 36 36 $f = $parent['title']; 37 37 $child = $parent['author']; 38 39 // ArrayAccess interface40 $t->diag('ArrayAccess interface');41 $t->is(isset($parent['title']), true, 'sfFormField implements the ArrayAccess interface');42 $t->is(isset($parent['title1']), false, 'sfFormField implements the ArrayAccess interface');43 $t->is($parent['title'], $f, 'sfFormField implements the ArrayAccess interface');44 try45 {46 unset($parent['title']);47 $t->fail('sfFormField implements the ArrayAccess interface but in read-only mode');48 }49 catch (LogicException $e)50 {51 $t->pass('sfFormField implements the ArrayAccess interface but in read-only mode');52 }53 54 try55 {56 $parent['title'] = null;57 $t->fail('sfFormField implements the ArrayAccess interface but in read-only mode');58 }59 catch (LogicException $e)60 {61 $t->pass('sfFormField implements the ArrayAccess interface but in read-only mode');62 }63 64 try65 {66 $f['title'];67 $t->fail('sfFormField implements the ArrayAccess interface but in read-only mode');68 }69 catch (LogicException $e)70 {71 $t->pass('sfFormField implements the ArrayAccess interface but in read-only mode');72 }73 74 try75 {76 $parent['title1'];77 $t->fail('sfFormField implements the ArrayAccess interface but in read-only mode');78 }79 catch (LogicException $e)80 {81 $t->pass('sfFormField implements the ArrayAccess interface but in read-only mode');82 }83 38 84 39 // ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() … … 92 47 $errorSchema1 = new sfValidatorErrorSchema(new sfValidatorString()); 93 48 $errorSchema1->addError(new sfValidatorError(new sfValidatorString(), 'error'), 'title1'); 94 $parent1 = new sfFormField ($schema, null, 'article', array('title' => 'symfony'), $errorSchema1);49 $parent1 = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony'), $errorSchema1); 95 50 $f1 = $parent1['title']; 96 51 $t->is($f1->hasError(), false, '->hasError() returns false if the form field has no error');

