Development

#4567: patch.diff

You must first sign up to be able to contribute.

Ticket #4567: patch.diff

File patch.diff, 7.1 kB (added by pablodip, 3 months ago)
  • test/unit/widget/sfWidgetFormSchemaTest.php

    old new  
    1010 
    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')); 
    1616$w2 = new sfWidgetFormInput(); 
     
    358358  $t->ok($widget == $f[$name], '__clone() clones embedded widgets'); 
    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()'); 
    363379$w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); 
  • lib/widget/sfWidgetFormSchemaFormatter.class.php

    old new  
    1818 */ 
    1919abstract class sfWidgetFormSchemaFormatter 
    2020{ 
    21   protected static  
     21  protected static 
    2222    $translationCallable       = null; 
    23    
     23 
    2424  protected 
    2525    $rowFormat                 = '', 
    2626    $helpFormat                = '%help%', 
     
    3939   */ 
    4040  public function __construct(sfWidgetFormSchema $widgetSchema) 
    4141  { 
     42    $this->setWidgetSchema($widgetSchema); 
     43  } 
     44 
     45  public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 
     46  { 
    4247    $this->widgetSchema = $widgetSchema; 
    4348  } 
    44      
     49 
     50  public function getWidgetSchema() 
     51  { 
     52    return $this->widgetSchema; 
     53  } 
     54 
    4555  public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 
    4656  { 
    4757    return strtr($this->getRowFormat(), array( 
     
    5262      '%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields%' : $hiddenFields, 
    5363    )); 
    5464  } 
    55    
     65 
    5666  /** 
    5767   * Translates a string using an i18n callable, if it has been provided 
    5868   * 
     
    6474  { 
    6575    if (false === $subject) 
    6676    { 
    67       return false;   
     77      return false; 
    6878    } 
    6979 
    7080    if (is_null(self::$translationCallable)) 
     
    8090 
    8191      return strtr($subject, $parameters); 
    8292    } 
    83      
     93 
    8494    $catalogue = $this->getTranslationCatalogue(); 
    8595 
    8696    if (self::$translationCallable instanceof sfCallable) 
     
    100110  { 
    101111    return self::$translationCallable; 
    102112  } 
    103    
     113 
    104114  /** 
    105    * Sets a callable which aims to translate form labels, errors and help messages  
     115   * Sets a callable which aims to translate form labels, errors and help messages 
    106116   * 
    107117   * @param  mixed  $callable 
    108    *  
     118   * 
    109119   * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 
    110120   */ 
    111121  static public function setTranslationCallable($callable) 
     
    114124    { 
    115125      throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 
    116126    } 
    117      
     127 
    118128    self::$translationCallable = $callable; 
    119129  } 
    120    
     130 
    121131  public function formatHelp($help) 
    122132  { 
    123133    if (!$help) 
     
    152162 
    153163    return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 
    154164  } 
    155    
     165 
    156166  /** 
    157167   * Generates a label for the given field name. 
    158168   * 
     
    169179    { 
    170180      return ''; 
    171181    } 
    172      
     182 
    173183    $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 
    174184    $attributes = array_merge($attributes, array('for' => $widgetId)); 
    175      
     185 
    176186    return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 
    177187  } 
    178188 
     
    186196  public function generateLabelName($name) 
    187197  { 
    188198    $label = $this->widgetSchema->getLabel($name); 
    189      
     199 
    190200    if (!$label && false !== $label) 
    191201    { 
    192202      $label = str_replace('_', ' ', ucfirst($name)); 
     
    194204 
    195205    return $this->translate($label); 
    196206  } 
    197    
     207 
    198208  /** 
    199209   * Get i18n catalogue name 
    200210   * 
     
    204214  { 
    205215    return $this->translationCatalogue; 
    206216  } 
    207    
     217 
    208218  /** 
    209219   * Set an i18n catalogue name 
    210220   * 
     
    217227    { 
    218228      throw new InvalidArgumentException('Catalogue name must be a string'); 
    219229    } 
    220      
     230 
    221231    $this->translationCatalogue = $catalogue; 
    222232  } 
    223    
     233 
    224234  protected function unnestErrors($errors, $prefix = '') 
    225235  { 
    226236    $newErrors = array(); 
     
    255265 
    256266    return $newErrors; 
    257267  } 
    258    
     268 
    259269  public function setRowFormat($format) 
    260270  { 
    261271    $this->rowFormat = $format; 
  • lib/widget/sfWidgetFormSchema.class.php

    old new  
    2525    LAST   = 'last', 
    2626    BEFORE = 'before', 
    2727    AFTER  = 'after'; 
    28    
     28 
    2929  protected static 
    3030    $defaultFormatterName = 'table'; 
    3131 
     
    102102  { 
    103103    return $this->formFormatters; 
    104104  } 
    105    
     105 
    106106  /** 
    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  
     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 
    113113   * { 
     
    116116   *     sfWidgetFormSchema::setDefaultFormFormatterName('list'); 
    117117   *   } 
    118118   * } 
    119    * </pre>   
     119   * </pre> 
    120120   * 
    121121   * @param string $name  New default formatter name 
    122122   */ 
     
    159159    if (!isset($this->formFormatters[$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  } 
    173173 
     
    675675      // offsetSet will clone the field and change the parent 
    676676      $this[$name] = $field; 
    677677    } 
     678 
     679    foreach ($this->formFormatters as &$formFormatter) 
     680    { 
     681      $formFormatter = clone $formFormatter; 
     682      $formFormatter->setWidgetSchema($this); 
     683    } 
    678684  } 
    679685}