Development

Changeset 33485

You must first sign up to be able to contribute.

Changeset 33485

Show
Ignore:
Timestamp:
07/06/12 16:46:06 (11 months ago)
Author:
alvarola
Message:

Adding embedMergeForm that allows to simulate merge while embeding forms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/dcReloadedFormExtraPlugin/trunk/lib/form/pmFormPropel.class.php

    r33484 r33485  
    2424   
    2525  public function configureValidators() {} 
     26 
     27  /** 
     28   * Embeds a form like "mergeForm" does, but will still 
     29   * save the input data. 
     30   */ 
     31  public function embedMergeForm($name, sfForm $form) 
     32  { 
     33    // This starts like sfForm::embedForm 
     34    $name = (string) $name; 
     35    if (true === $this->isBound() || true === $form->isBound()) 
     36    { 
     37      throw new LogicException('A bound form cannot be merged'); 
     38    } 
     39     
     40    $this->embeddedForms[$name] = $form; 
     41 
     42    $form = clone $form; 
     43    unset($form[self::$CSRFFieldName]); 
     44 
     45    // But now, copy each widget instead of the while form into the current 
     46    // form. Each widget ist named "formname-fieldname". 
     47      foreach ($form->getWidgetSchema()->getFields() as $field => $widget) 
     48    { 
     49      $widgetName = "$name-$field"; 
     50      if (isset($this->widgetSchema[$widgetName])) 
     51      { 
     52        throw new LogicException("The forms cannot be merged. A field name '$widgetName' already exists."); 
     53      } 
     54 
     55      $this->widgetSchema[$widgetName] = $widget;                           // Copy widget 
     56      $this->validatorSchema[$widgetName] = $form->validatorSchema[$field]; // Copy schema 
     57      $this->setDefault($widgetName, $form->getDefault($field));            // Copy default value 
     58      $this->widgetSchema->setHelp($widgetName, $form->getWidgetSchema()->getHelp($field)); // Copy help 
     59 
     60      if (!$widget->getLabel()) 
     61      { 
     62        // Re-create label if not set (otherwise it would be named 'ucfirst($widgetName)') 
     63        $label = $form->getWidgetSchema()->getFormFormatter()->generateLabelName($field); 
     64        $this->getWidgetSchema()->setLabel($widgetName, $label); 
     65      } 
     66    } 
     67 
     68    $this->mergePreValidator($form->getValidatorSchema()->getPreValidator()); 
     69    $this->mergePostValidator($form->getValidatorSchema()->getPostValidator()); 
     70 
     71    // And this is like in sfForm::embedForm 
     72    $this->resetFormFields(); 
     73 
     74  } 
     75 
     76   /** 
     77   * Override sfFormDoctrine to prepare the 
     78   * values: FORMNAME-FIELDNAME has to be transformed 
     79   * to FORMNAME[FIELDNAME] 
     80   */ 
     81  public function updateObject($values = null) 
     82  { 
     83    if (is_null($values)) 
     84    { 
     85      $values = $this->values; 
     86      foreach ($this->embeddedForms AS $name => $form) 
     87      { 
     88        foreach ($form AS $field => $f) 
     89        { 
     90          if (isset($values["$name-$field"])) 
     91          { 
     92            // Re-rename the form field and remove 
     93            // the original field 
     94            $values[$name][$field] = $values["$name-$field"]; 
     95            unset($values["$name-$field"]); 
     96          } 
     97        } 
     98      } 
     99    } 
     100 
     101    // Give the request to the original method 
     102    parent::updateObject($values); 
     103  } 
    26104}