diff --git a/src/Controllers/ElementalAreaController.php b/src/Controllers/ElementalAreaController.php index db7a7886..014e9a9f 100644 --- a/src/Controllers/ElementalAreaController.php +++ b/src/Controllers/ElementalAreaController.php @@ -298,8 +298,7 @@ public function getElementForm(): Form } /** - * Arrive here from FormRequestHandler::httpSubmission() during a POST request to - * /admin/linkfield/linkForm/ + * Arrive here from FormRequestHandler::httpSubmission() during a POST request. * The 'save' method is called because it is the FormAction set on the Form */ public function save(array $data, Form $form): HTTPResponse @@ -332,10 +331,10 @@ public function save(array $data, Form $form): HTTPResponse } // Remove the namespace prefixes that were added by EditFormFactory - $dataWithoutNamespaces = static::removeNamespacesFromFields($data, $element->ID); + static::removeNamespacesFromFields($form, $element->ID); // Update and write the data object which will trigger model validation - $element->updateFromFormData($dataWithoutNamespaces); + $form->saveInto($element); if ($element->isChanged()) { try { $element->write(); @@ -354,25 +353,19 @@ public function save(array $data, Form $form): HTTPResponse /** * Remove the pseudo namespaces that were added to form fields by the form factory - * - * @param array $data - * @param int $elementID - * @return array */ - public static function removeNamespacesFromFields(array $data, $elementID) + private function removeNamespacesFromFields(Form $form, int $elementID): void { - $output = []; $template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, ''); - foreach ($data as $key => $value) { + $saveableFields = $form->Fields()->saveableFields(); + foreach ($saveableFields as $namespacedName => $field) { // Only look at fields that match the namespace template - if (substr($key ?? '', 0, strlen($template ?? '')) !== $template) { + if (substr($namespacedName, 0, strlen($template)) !== $template) { continue; } - - $fieldName = substr($key ?? '', strlen($template ?? '')); - $output[$fieldName] = $value; + $newName = substr($namespacedName, strlen($template)); + $field->setName($newName); } - return $output; } private function reorderElements(BaseElement $element, int $afterElementID): void diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index b2e59996..dc1adccf 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -646,27 +646,6 @@ public function getRenderTemplates($suffix = '') return $templateFlat; } - /** - * Given form data (wit - * - * @param $data - */ - public function updateFromFormData($data) - { - $cmsFields = $this->getCMSFields(); - - foreach ($data as $field => $datum) { - $field = $cmsFields->dataFieldByName($field); - - if (!$field) { - continue; - } - - $field->setSubmittedValue($datum); - $field->saveInto($this); - } - } - /** * Strip all namespaces from class namespace. *