Development

Changeset 7047

You must first sign up to be able to contribute.

Changeset 7047

Show
Ignore:
Timestamp:
01/14/08 14:50:17 (9 months ago)
Author:
fabien
Message:

splitted sfFormField in sfFormField and sfFormFieldSchema

Files:

Legend:

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

    r6990 r7047  
    573573      $values = $this->isBound ? $this->taintedValues : $this->defaults; 
    574574 
    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]); 
    576578    } 
    577579 
     
    609611   * Returns a form field for the main widget schema. 
    610612   * 
    611    * @return sfFormField A sfFormField instance 
     613   * @return sfFormFieldSchema A sfFormFieldSchema instance 
    612614   */ 
    613615  protected function getFormField() 
     
    615617    if (is_null($this->formField)) 
    616618    { 
    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); 
    618620    } 
    619621 
  • branches/1.1/lib/form/sfFormField.class.php

    r7045 r7047  
    1717 * @version    SVN: $Id$ 
    1818 */ 
    19 class sfFormField implements ArrayAccess 
     19class sfFormField 
    2020{ 
    2121  protected 
     
    2424    $name   = '', 
    2525    $value  = null, 
    26     $error  = null, 
    27     $fields = array(); 
     26    $error  = null; 
    2827 
    2928  /** 
    3029   * Constructor. 
    3130   * 
    32    * @param sfWidget         A sfWidget instance 
     31   * @param sfWidgetForm     A sfWidget instance 
    3332   * @param sfFormField      The sfFormField parent instance (null for the root widget) 
    3433   * @param string           The field name 
     
    3635   * @param sfValidatorError A sfValidatorError instance 
    3736   */ 
    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) 
    3938  { 
    4039    $this->widget = $widget; 
     
    202201    return !is_null($this->error) && count($this->error); 
    203202  } 
    204  
    205   /** 
    206    * Returns true if the bound field exists (implements the ArrayAccess interface). 
    207    * 
    208    * @param  string  The name of the bound field 
    209    * 
    210    * @return Boolean true if the widget exists, false otherwise 
    211    */ 
    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 get 
    221    * 
    222    * @return sfFormField A form field instance 
    223    */ 
    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 LogicException 
    263    */ 
    264   public function offsetUnset($offset) 
    265   { 
    266     throw new LogicException('Cannot remove form fields (read-only).'); 
    267   } 
    268203} 
  • branches/1.1/test/unit/form/sfFormFieldTest.php

    r7045 r7047  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(28, new lime_output_color()); 
     13$t = new lime_test(21, new lime_output_color()); 
    1414 
    1515// widgets 
     
    3333$articleErrorSchema->addError($authorErrorSchema, 'author'); 
    3434 
    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); 
    3636$f = $parent['title']; 
    3737$child = $parent['author']; 
    38  
    39 // ArrayAccess interface 
    40 $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 try 
    45 { 
    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 try 
    55 { 
    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 try 
    65 { 
    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 try 
    75 { 
    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 } 
    8338 
    8439// ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() 
     
    9247$errorSchema1 = new sfValidatorErrorSchema(new sfValidatorString()); 
    9348$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); 
    9550$f1 = $parent1['title']; 
    9651$t->is($f1->hasError(), false, '->hasError() returns false if the form field has no error');