Development

Changeset 11961

You must first sign up to be able to contribute.

Changeset 11961

Show
Ignore:
Timestamp:
10/05/08 20:08:57 (6 years ago)
Author:
fabien
Message:

fixed sfWidgetFormSchema don't clone the formatters (closes #4567 - patch from pablodip)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/widget/sfWidgetFormSchema.class.php

    r11509 r11961  
    2626    BEFORE = 'before', 
    2727    AFTER  = 'after'; 
    28    
     28 
    2929  protected static 
    3030    $defaultFormatterName = 'table'; 
     
    103103    return $this->formFormatters; 
    104104  } 
    105    
    106   /** 
    107    * Sets the generic default formatter name used by the class. If you want all  
    108    * of your forms to be generated with the <code>list</code> format, you can  
     105 
     106  /** 
     107   * Sets the generic default formatter name used by the class. If you want all 
     108   * of your forms to be generated with the <code>list</code> format, you can 
    109109   * do it in a project or application configuration class: 
    110    *  
     110   * 
    111111   * <pre> 
    112112   * class ProjectConfiguration extends sfProjectConfiguration 
     
    117117   *   } 
    118118   * } 
    119    * </pre>   
     119   * </pre> 
    120120   * 
    121121   * @param string $name  New default formatter name 
     
    160160    { 
    161161      $class = 'sfWidgetFormSchemaFormatter'.ucfirst($name); 
    162        
     162 
    163163      if (!class_exists($class)) 
    164164      { 
    165165        throw new InvalidArgumentException(sprintf('The form formatter "%s" does not exist.', $name)); 
    166166      } 
    167        
     167 
    168168      $this->formFormatters[$name] = new $class($this); 
    169169    } 
    170      
     170 
    171171    return $this->formFormatters[$name]; 
    172172  } 
     
    676676      $this[$name] = $field; 
    677677    } 
     678 
     679    foreach ($this->formFormatters as &$formFormatter) 
     680    { 
     681      $formFormatter = clone $formFormatter; 
     682      $formFormatter->setWidgetSchema($this); 
     683    } 
    678684  } 
    679685} 
  • branches/1.1/lib/widget/sfWidgetFormSchemaFormatter.class.php

    r11509 r11961  
    1919abstract class sfWidgetFormSchemaFormatter 
    2020{ 
    21   protected static  
     21  protected static 
    2222    $translationCallable       = null; 
    23    
     23 
    2424  protected 
    2525    $rowFormat                 = '', 
     
    4040  public function __construct(sfWidgetFormSchema $widgetSchema) 
    4141  { 
    42     $this->widgetSchema = $widgetSchema
    43   } 
    44      
     42    $this->setWidgetSchema($widgetSchema)
     43  } 
     44 
    4545  public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 
    4646  { 
     
    5353    )); 
    5454  } 
    55    
     55 
    5656  /** 
    5757   * Translates a string using an i18n callable, if it has been provided 
     
    6565    if (false === $subject) 
    6666    { 
    67       return false;   
     67      return false; 
    6868    } 
    6969 
     
    8181      return strtr($subject, $parameters); 
    8282    } 
    83      
     83 
    8484    $catalogue = $this->getTranslationCatalogue(); 
    8585 
     
    101101    return self::$translationCallable; 
    102102  } 
    103    
    104   /** 
    105    * Sets a callable which aims to translate form labels, errors and help messages  
     103 
     104  /** 
     105   * Sets a callable which aims to translate form labels, errors and help messages 
    106106   * 
    107107   * @param  mixed  $callable 
    108    *  
     108   * 
    109109   * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 
    110110   */ 
     
    115115      throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 
    116116    } 
    117      
     117 
    118118    self::$translationCallable = $callable; 
    119119  } 
    120    
     120 
    121121  public function formatHelp($help) 
    122122  { 
     
    153153    return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 
    154154  } 
    155    
     155 
    156156  /** 
    157157   * Generates a label for the given field name. 
     
    170170      return ''; 
    171171    } 
    172      
     172 
    173173    $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 
    174174    $attributes = array_merge($attributes, array('for' => $widgetId)); 
    175      
     175 
    176176    return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 
    177177  } 
     
    187187  { 
    188188    $label = $this->widgetSchema->getLabel($name); 
    189      
     189 
    190190    if (!$label && false !== $label) 
    191191    { 
     
    195195    return $this->translate($label); 
    196196  } 
    197    
     197 
    198198  /** 
    199199   * Get i18n catalogue name 
     
    205205    return $this->translationCatalogue; 
    206206  } 
    207    
     207 
    208208  /** 
    209209   * Set an i18n catalogue name 
     
    218218      throw new InvalidArgumentException('Catalogue name must be a string'); 
    219219    } 
    220      
     220 
    221221    $this->translationCatalogue = $catalogue; 
    222222  } 
    223    
     223 
    224224  protected function unnestErrors($errors, $prefix = '') 
    225225  { 
     
    256256    return $newErrors; 
    257257  } 
    258    
     258 
    259259  public function setRowFormat($format) 
    260260  { 
     
    326326    return $this->helpFormat; 
    327327  } 
     328 
     329  /** 
     330   * Sets the widget schema associated with this formatter instance. 
     331   * 
     332   * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance 
     333   */ 
     334  public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 
     335  { 
     336    $this->widgetSchema = $widgetSchema; 
     337  } 
     338 
     339  public function getWidgetSchema() 
     340  { 
     341    return $this->widgetSchema; 
     342  } 
    328343} 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php

    r11509 r11961  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(61, new lime_output_color()); 
     13$t = new lime_test(70, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 
     
    359359} 
    360360 
     361$w = new sfWidgetFormSchema(); 
     362$w->addFormFormatter('table', new sfWidgetFormSchemaFormatterTable($w)); 
     363$w->addFormFormatter('list', new sfWidgetFormSchemaFormatterList($w)); 
     364$w1 = clone $w; 
     365$f1 = $w1->getFormFormatters(); 
     366$f = $w->getFormFormatters(); 
     367$t->is(array_keys($f1), array_keys($f), '__clone() clones form formatters'); 
     368foreach ($f1 as $key => $formFormatter) 
     369{ 
     370  $t->ok($formFormatter !== $f[$key], '__clone() clones form formatters'); 
     371  $t->is(get_class($formFormatter), get_class($f[$key]), '__clone() clones form formatters'); 
     372 
     373  $t->ok($formFormatter->getWidgetSchema() !== $f[$key]->getWidgetSchema(), '__clone() clones form formatters'); 
     374  $t->is(get_class($formFormatter->getWidgetSchema()), get_class($f[$key]->getWidgetSchema()), '__clone() clones form formatters'); 
     375} 
     376 
    361377// setDefaultFormFormatterName() 
    362378$t->diag('setDefaultFormFormatterName()'); 
  • branches/1.2/lib/widget/sfWidgetFormSchema.class.php

    r11362 r11961  
    2626    BEFORE = 'before', 
    2727    AFTER  = 'after'; 
    28    
     28 
    2929  protected static 
    3030    $defaultFormatterName = 'table'; 
     
    103103    return $this->formFormatters; 
    104104  } 
    105    
    106   /** 
    107    * Sets the generic default formatter name used by the class. If you want all  
    108    * of your forms to be generated with the <code>list</code> format, you can  
     105 
     106  /** 
     107   * Sets the generic default formatter name used by the class. If you want all 
     108   * of your forms to be generated with the <code>list</code> format, you can 
    109109   * do it in a project or application configuration class: 
    110    *  
     110   * 
    111111   * <pre> 
    112112   * class ProjectConfiguration extends sfProjectConfiguration 
     
    117117   *   } 
    118118   * } 
    119    * </pre>   
     119   * </pre> 
    120120   * 
    121121   * @param string $name  New default formatter name 
     
    160160    { 
    161161      $class = 'sfWidgetFormSchemaFormatter'.ucfirst($name); 
    162        
     162 
    163163      if (!class_exists($class)) 
    164164      { 
    165165        throw new InvalidArgumentException(sprintf('The form formatter "%s" does not exist.', $name)); 
    166166      } 
    167        
     167 
    168168      $this->formFormatters[$name] = new $class($this); 
    169169    } 
    170      
     170 
    171171    return $this->formFormatters[$name]; 
    172172  } 
     
    681681      $formFormatter->setWidgetSchema($this); 
    682682    } 
     683 
     684    foreach ($this->formFormatters as &$formFormatter) 
     685    { 
     686      $formFormatter = clone $formFormatter; 
     687      $formFormatter->setWidgetSchema($this); 
     688    } 
    683689  } 
    684690} 
  • branches/1.2/lib/widget/sfWidgetFormSchemaFormatter.class.php

    r11362 r11961  
    1919abstract class sfWidgetFormSchemaFormatter 
    2020{ 
    21   protected static  
     21  protected static 
    2222    $translationCallable       = null; 
    23    
     23 
    2424  protected 
    2525    $rowFormat                 = '', 
     
    4242    $this->setWidgetSchema($widgetSchema); 
    4343  } 
    44      
     44 
    4545  public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 
    4646  { 
     
    5353    )); 
    5454  } 
    55    
     55 
    5656  /** 
    5757   * Translates a string using an i18n callable, if it has been provided 
     
    6565    if (false === $subject) 
    6666    { 
    67       return false;   
     67      return false; 
    6868    } 
    6969 
     
    8181      return strtr($subject, $parameters); 
    8282    } 
    83      
     83 
    8484    $catalogue = $this->getTranslationCatalogue(); 
    8585 
     
    101101    return self::$translationCallable; 
    102102  } 
    103    
    104   /** 
    105    * Sets a callable which aims to translate form labels, errors and help messages  
     103 
     104  /** 
     105   * Sets a callable which aims to translate form labels, errors and help messages 
    106106   * 
    107107   * @param  mixed  $callable 
    108    *  
     108   * 
    109109   * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 
    110110   */ 
     
    115115      throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 
    116116    } 
    117      
     117 
    118118    self::$translationCallable = $callable; 
    119119  } 
    120    
     120 
    121121  public function formatHelp($help) 
    122122  { 
     
    153153    return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 
    154154  } 
    155    
     155 
    156156  /** 
    157157   * Generates a label for the given field name. 
     
    170170      return ''; 
    171171    } 
    172      
     172 
    173173    $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 
    174174    $attributes = array_merge($attributes, array('for' => $widgetId)); 
    175      
     175 
    176176    return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 
    177177  } 
     
    187187  { 
    188188    $label = $this->widgetSchema->getLabel($name); 
    189      
     189 
    190190    if (!$label && false !== $label) 
    191191    { 
     
    195195    return $this->translate($label); 
    196196  } 
    197    
     197 
    198198  /** 
    199199   * Get i18n catalogue name 
     
    205205    return $this->translationCatalogue; 
    206206  } 
    207    
     207 
    208208  /** 
    209209   * Set an i18n catalogue name 
     
    218218      throw new InvalidArgumentException('Catalogue name must be a string'); 
    219219    } 
    220      
     220 
    221221    $this->translationCatalogue = $catalogue; 
    222222  } 
    223    
     223 
    224224  protected function unnestErrors($errors, $prefix = '') 
    225225  { 
     
    256256    return $newErrors; 
    257257  } 
    258    
     258 
    259259  public function setRowFormat($format) 
    260260  { 
     
    327327  } 
    328328 
    329   public function setWidgetSchema($widgetSchema) 
     329  /** 
     330   * Sets the widget schema associated with this formatter instance. 
     331   * 
     332   * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance 
     333   */ 
     334  public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 
    330335  { 
    331336    $this->widgetSchema = $widgetSchema; 
    332337  } 
     338 
     339  public function getWidgetSchema() 
     340  { 
     341    return $this->widgetSchema; 
     342  } 
    333343} 
  • branches/1.2/test/unit/widget/sfWidgetFormSchemaTest.php

    r11362 r11961  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(63, new lime_output_color()); 
     13$t = new lime_test(72, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 
     
    366366$t->is($formatters['testFormatter']->getTranslationCatalogue(), 'english', '__clone() clones formatters, so that changes to the original one have no effect to the cloned formatter.'); 
    367367 
     368$w = new sfWidgetFormSchema(); 
     369$w->addFormFormatter('table', new sfWidgetFormSchemaFormatterTable($w)); 
     370$w->addFormFormatter('list', new sfWidgetFormSchemaFormatterList($w)); 
     371$w1 = clone $w; 
     372$f1 = $w1->getFormFormatters(); 
     373$f = $w->getFormFormatters(); 
     374$t->is(array_keys($f1), array_keys($f), '__clone() clones form formatters'); 
     375foreach ($f1 as $key => $formFormatter) 
     376{ 
     377  $t->ok($formFormatter !== $f[$key], '__clone() clones form formatters'); 
     378  $t->is(get_class($formFormatter), get_class($f[$key]), '__clone() clones form formatters'); 
     379 
     380  $t->ok($formFormatter->getWidgetSchema() !== $f[$key]->getWidgetSchema(), '__clone() clones form formatters'); 
     381  $t->is(get_class($formFormatter->getWidgetSchema()), get_class($f[$key]->getWidgetSchema()), '__clone() clones form formatters'); 
     382} 
     383 
    368384// setDefaultFormFormatterName() 
    369385$t->diag('setDefaultFormFormatterName()');