Skip to content

Commit

Permalink
API Remove custom logic in favour of Form::saveInto()
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 6, 2024
1 parent eadd5ad commit 13c1efe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 54 deletions.
38 changes: 9 additions & 29 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ public function getElementForm(): Form
}

/**
* Arrive here from FormRequestHandler::httpSubmission() during a POST request to
* /admin/linkfield/linkForm/<LinkID>
* 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
Expand Down Expand Up @@ -331,11 +330,15 @@ public function save(array $data, Form $form): HTTPResponse
$this->jsonError(403);
}

// Remove the namespace prefixes that were added by EditFormFactory
$dataWithoutNamespaces = static::removeNamespacesFromFields($data, $element->ID);
// Remove the namespace prefixes
$factory = Injector::inst()->get(EditFormFactory::class);
$factory->removeNamespaceFromFields($form->Fields(), ['Record' => $element]);
// Update the record
$form->saveInto($element);
// Add the namespace prefixes back - this is necessary for the response to be handled correctly
$factory->namespaceFields($form->Fields(), ['Record' => $element]);

// Update and write the data object which will trigger model validation
$element->updateFromFormData($dataWithoutNamespaces);
// Write the data object which will trigger model validation
if ($element->isChanged()) {
try {
$element->write();
Expand All @@ -352,29 +355,6 @@ public function save(array $data, Form $form): HTTPResponse
return $response;
}

/**
* 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)
{
$output = [];
$template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, '');
foreach ($data as $key => $value) {
// Only look at fields that match the namespace template
if (substr($key ?? '', 0, strlen($template ?? '')) !== $template) {
continue;
}

$fieldName = substr($key ?? '', strlen($template ?? ''));
$output[$fieldName] = $value;
}
return $output;
}

private function reorderElements(BaseElement $element, int $afterElementID): void
{
if ($afterElementID < 0) {
Expand Down
23 changes: 19 additions & 4 deletions src/Forms/EditFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ protected function getFormValidator(RequestHandler $controller = null, $name, $c
/**
* Given a {@link FieldList}, give all fields a unique name so they can be used in the same context as
* other elemental edit forms and the page (or other DataObject) that owns them.
*
* @param FieldList $fields
* @param array $context
*/
protected function namespaceFields(FieldList $fields, array $context)
public function namespaceFields(FieldList $fields, array $context): void
{
$elementID = $context['Record']->ID;

Expand All @@ -103,4 +100,22 @@ protected function namespaceFields(FieldList $fields, array $context)
$field->setName($namespacedName);
}
}

/**
* Remove the pseudo namespaces that were added in namespaceFields()
*/
public function removeNamespaceFromFields(FieldList $fields, array $context): void
{
$elementID = $context['Record']->ID;
$template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, '');

foreach ($fields->dataFields() as $namespacedName => $field) {
// Only look at fields that match the namespace template
if (substr($namespacedName, 0, strlen($template)) !== $template) {
continue;
}
$newName = substr($namespacedName, strlen($template));
$field->setName($newName);
}
}
}
28 changes: 7 additions & 21 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ public function write(
return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents, $skipValidation);
}

public function validate()
{
$result = parent::validate();
$result->addFieldError('Title', 'AAAHHH');
return $result;
}

protected function onBeforeWrite()
{
parent::onBeforeWrite();
Expand Down Expand Up @@ -646,27 +653,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.
*
Expand Down

0 comments on commit 13c1efe

Please sign in to comment.