/**
  * Here we just drop the embedded creation forms if no value has been
  * provided for them (this simulates a non-required embedded form),
  * please provide the fields for the related embedded form in the call
  * to $this->embedRelations() so we don't throw validation errors
  * if the user did not want to add a new related object
  *
  * @see sfForm::doBind()
  */
 protected function doBind(array $values)
 {
     foreach ($this->embedRelations as $relationName => $keys) {
         $keys = $this->addDefaultRelationSettings($keys);
         if ($keys['sortable'] !== false && isset($values[$relationName]) && is_array($values[$relationName])) {
             $values[$relationName] = $this->reorderSubformValues($values[$relationName], $keys['sortable'], $keys, $relationName);
         }
         if (!$keys['noNewForm']) {
             $containerName = 'new_' . $relationName;
             if ($keys['multipleNewForms']) {
                 if (array_key_exists($containerName, $values)) {
                     foreach ($values[$containerName] as $index => $subFormValues) {
                         if ($this->isNewFormEmpty($subFormValues, $keys)) {
                             if (isset($this->embeddedForms[$containerName][$index])) {
                                 // we remember this form to be removed later (it's too early now)
                                 $this->newObjectFormsToIgnore[] = $this->embeddedForms[$containerName][$index];
                             }
                             unset($values[$containerName][$index]);
                             unset($this->validatorSchema[$containerName][$index]);
                         } else {
                             // if new forms were inserted client-side, embed them here
                             if (!isset($this->embeddedForms[$containerName][$index])) {
                                 // create and embed new form
                                 $relation = $this->getObject()->getTable()->getRelation($relationName);
                                 $addedForm = $this->embeddedFormFactory($relationName, $keys, $relation, (int) $index + 1);
                                 $ef = $this->embeddedForms[$containerName];
                                 $ef->embedForm($index, $addedForm);
                                 // ... and reset other stuff (symfony loses all this since container form is already embedded)
                                 $this->validatorSchema[$containerName] = $ef->getValidatorSchema();
                                 $this->widgetSchema[$containerName] = new sfWidgetFormSchemaDecorator($ef->getWidgetSchema(), $ef->getWidgetSchema()->getFormFormatter()->getDecoratorFormat());
                                 $this->setDefault($containerName, $ef->getDefaults());
                             }
                             // we're keeping track (maybe a bit too much info) of what we need to care about later:
                             $relation = $this->getObject()->getTable()->getRelation($relationName);
                             $subforms = $this->embeddedForms[$containerName];
                             $this->scheduledForInsertion[$containerName][$index] = array('form' => $subforms->embeddedForms[$index], 'relation' => $relation, 'relationName' => $relationName, 'container' => $containerName);
                         }
                     }
                 }
                 $this->validatorSchema[$containerName] = $this->embeddedForms[$containerName]->getValidatorSchema();
                 // check for new forms that were deleted client-side and never submitted
                 if (array_key_exists($containerName, $values)) {
                     foreach (array_keys($this->embeddedForms[$containerName]->embeddedForms) as $index) {
                         if (!array_key_exists($index, $values[$containerName])) {
                             unset($this->embeddedForms[$containerName][$index]);
                             unset($this->validatorSchema[$containerName][$index]);
                         }
                     }
                 }
                 if (!array_key_exists($containerName, $values) || count($values[$containerName]) === 0) {
                     unset($values[$containerName], $this->validatorSchema[$containerName]);
                 }
             } else {
                 if (!array_key_exists($containerName, $values) || $this->isNewFormEmpty($values[$containerName], $keys)) {
                     if (isset($this->embeddedForms[$containerName])) {
                         // we remember this form to be removed later (it's too early now)
                         $this->newObjectFormsToIgnore[] = $this->embeddedForms[$containerName];
                     }
                     unset($values[$containerName], $this->validatorSchema[$containerName]);
                 } else {
                     $this->scheduledForInsertion[$containerName][0] = array('form' => $this->embeddedForms[$containerName], 'relation' => $this->getObject()->getTable()->getRelation($relationName), 'relationName' => $relationName, 'container' => $containerName);
                 }
             }
         }
         if (isset($values[$relationName])) {
             $oneToOneRelationFix = $this->getObject()->getTable()->getRelation($relationName)->isOneToOne() ? array($values[$relationName]) : $values[$relationName];
             foreach ($oneToOneRelationFix as $i => $relationValues) {
                 if (isset($relationValues['delete_object']) && $relationValues['id']) {
                     $this->scheduledForDeletion[$relationName][$i] = $relationValues['id'];
                 }
             }
         }
     }
     parent::doBind($values);
 }
 /**
  * Here we just drop the embedded creation forms if no value has been
  * provided for them (this simulates a non-required embedded form),
  * please provide the fields for the related embedded form in the call
  * to $this->embedRelations() so we don't throw validation errors
  * if the user did not want to add a new related object
  *
  * @see sfForm::doBind()
  */
 protected function doBind(array $values)
 {
     foreach ($this->embedRelations as $relationName => $keys) {
         $keys = $this->addDefaultRelationSettings($keys);
         if (!$keys['noNewForm']) {
             $containerName = 'new_' . $relationName;
             if ($keys['multipleNewForms']) {
                 if (array_key_exists($containerName, $values)) {
                     foreach ($values[$containerName] as $index => $subFormValues) {
                         if ($this->isNewFormEmpty($subFormValues, $keys)) {
                             unset($values[$containerName][$index], $this->embeddedForms[$containerName][$index]);
                             unset($this->validatorSchema[$containerName][$index]);
                         } else {
                             // if new forms were inserted client-side, embed them here
                             if (!isset($this->embeddedForms[$containerName][$index])) {
                                 // create and embed new form
                                 $relation = $this->getObject()->getTable()->getRelation($relationName);
                                 $addedForm = $this->embeddedFormFactory($relationName, $keys, $relation, (int) $index + 1);
                                 $ef = $this->embeddedForms[$containerName];
                                 $ef->embedForm($index, $addedForm);
                                 // ... and reset other stuff (symfony loses all this since container form is already embedded)
                                 $this->validatorSchema[$containerName] = $ef->getValidatorSchema();
                                 $this->widgetSchema[$containerName] = new sfWidgetFormSchemaDecorator($ef->getWidgetSchema(), $ef->getWidgetSchema()->getFormFormatter()->getDecoratorFormat());
                                 $this->setDefault($containerName, $ef->getDefaults());
                             }
                         }
                     }
                 }
                 $this->validatorSchema[$containerName] = $this->embeddedForms[$containerName]->getValidatorSchema();
                 // check for new forms that were deleted client-side and never submitted
                 if (array_key_exists($containerName, $values)) {
                     foreach (array_keys($this->embeddedForms[$containerName]->embeddedForms) as $index) {
                         if (!array_key_exists($index, $values[$containerName])) {
                             unset($this->embeddedForms[$containerName][$index]);
                             unset($this->validatorSchema[$containerName][$index]);
                         }
                     }
                 }
                 if (!array_key_exists($containerName, $values) || count($values[$containerName]) === 0) {
                     unset($values[$containerName], $this->validatorSchema[$containerName]);
                 }
             } else {
                 if (!array_key_exists($containerName, $values) || $this->isNewFormEmpty($values[$containerName], $keys)) {
                     unset($values[$containerName], $this->validatorSchema[$containerName]);
                 }
             }
         }
         if (isset($values[$relationName])) {
             $oneToOneRelationFix = $this->getObject()->getTable()->getRelation($relationName)->isOneToOne() ? array($values[$relationName]) : $values[$relationName];
             foreach ($oneToOneRelationFix as $i => $relationValues) {
                 if (isset($relationValues['delete_object']) && $relationValues['id']) {
                     $this->scheduledForDeletion[$relationName][$i] = $relationValues['id'];
                 }
             }
         }
     }
     parent::doBind($values);
 }