Skip to content

Instantly share code, notes, and snippets.

@ArnaudD
Created September 22, 2010 00:14
Show Gist options
  • Save ArnaudD/590851 to your computer and use it in GitHub Desktop.
Save ArnaudD/590851 to your computer and use it in GitHub Desktop.
Index: lib/form/sfFormPropel.class.php
===================================================================
--- lib/form/sfFormPropel.class.php (révision 30918)
+++ lib/form/sfFormPropel.class.php (copie de travail)
@@ -113,22 +113,70 @@
*/
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
- $this->addOptionalForms($taintedValues);
+ $this->addOptionalFormsRecusively($taintedValues);
return parent::bind($taintedValues, $taintedFiles);
}
-
- public function addOptionalForms($taintedValues = null)
+
+ /**
+ * Recursive function to populate a form with the optional forms added by the user
+ *
+ * @param array $taintedValues
+ * @param sfForm $form Form to populate. self if null
+ *
+ * @return boolean true if one of the embedded form changed
+ */
+ public function addOptionalFormsRecusively($taintedValues = null, $form = null)
+ {
+ if ($form === null)
+ $form = $this;
+
+ $changed = false;
+
+ foreach ($form->getEmbeddedForms() as $name => $embeddedForm)
+ {
+ if (array_key_exists($name, $taintedValues) &&
+ $this->addOptionalFormsRecusively($taintedValues[$name], $embeddedForm))
+ {
+ // the parent form schema is not updated when updating an embedded form
+ // so we must embed it again
+ $form->embedForm($name, $embeddedForm);
+ $changed = true;
+ }
+ }
+
+ if ($form instanceof sfFormPropel)
+ $changed |= $this->addOptionalForms ($taintedValues, $form);
+
+ return $changed;
+ }
+
+ /**
+ * Populate a form with the optional forms added by the user
+ *
+ * @param array $taintedValues
+ * @param sfForm $form Form to populate. self if null
+ *
+ * @return boolean true if one of the embedded form changed
+ */
+ public function addOptionalForms($taintedValues = null, $form = null)
{
- foreach ($this->optionalForms as $name => $form) {
+ if ($form === null)
+ $form = $this;
+
+ $changed = false;
+
+ foreach ($form->getOptionalForms() as $name => $optionalForm)
+ {
$i = 1;
if (strpos($name, '/') === false)
{
// The form must be added to the main form
while (array_key_exists($name . $i, $taintedValues))
{
- $this->embedForm($name . $i, clone $form);
- $this->getWidgetSchema()->moveField($name . $i, sfWidgetFormSchema::BEFORE, $name);
+ $form->embedForm($name . $i, clone $optionalForm);
+ $form->getWidgetSchema()->moveField($name . $i, sfWidgetFormSchema::BEFORE, $name);
$i++;
+ $changed = true;
}
}
else
@@ -140,18 +188,51 @@
continue;
}
$taintedValuesCopy = $taintedValues[$parent];
- $target = $this->embeddedForms[$parent];
+ $target = $form->getEmbeddedForm($parent);
while (array_key_exists($name . $i, $taintedValuesCopy))
{
- $target->embedForm($name . $i, clone $form);
+ $target->embedForm($name . $i, clone $optionalForm);
$target->getWidgetSchema()->moveField($name . $i, sfWidgetFormSchema::BEFORE, $name);
$i++;
// the parent form schema is not updated when updating an embedded form
// so we must embed it again
- $this->embedForm($parent, $target);
+ $form->embedForm($parent, $target);
+ $changed = true;
}
}
}
+
+ return $changed;
+ }
+
+
+ /**
+ * Gets the list of optional forms.
+ *
+ * @return array An array of Optional forms
+ */
+ public function getOptionalForms()
+ {
+ return $this->optionalForms;
+ }
+
+ /**
+ * Returns an Optional form.
+ *
+ * @param string $name The name used to embed the form
+ *
+ * @return sfForm
+ *
+ * @throws InvalidArgumentException If there is no form Optional with the supplied name
+ */
+ public function getOptionalForm($name)
+ {
+ if (!isset($this->optionalForms[$name]))
+ {
+ throw new InvalidArgumentException(sprintf('There is no optional form "%s".', $name));
+ }
+
+ return $this->optionalForms[$name];
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment