Skip to content

Commit

Permalink
SPIKE Inline validation
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 16, 2023
1 parent 768854b commit 595c436
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
use SilverStripe\Security\SecurityToken;
use SilverStripe\ORM\ValidationResult;

/**
* Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
Expand Down Expand Up @@ -115,8 +116,9 @@ public function getElementForm($elementID)
*/
public function apiSaveForm(HTTPRequest $request)
{
$id = $this->urlParams['ID'] ?? 0;
// Validate required input data
if (!isset($this->urlParams['ID'])) {
if ($id === 0) {
$this->jsonError(400);
return null;
}
Expand All @@ -139,7 +141,7 @@ public function apiSaveForm(HTTPRequest $request)
}

/** @var BaseElement $element */
$element = BaseElement::get()->byID($this->urlParams['ID']);
$element = BaseElement::get()->byID($id);
// Ensure the element can be edited by the current user
if (!$element || !$element->canEdit()) {
$this->jsonError(403);
Expand All @@ -149,6 +151,39 @@ public function apiSaveForm(HTTPRequest $request)
// Remove the pseudo namespaces that were added by the form factory
$data = $this->removeNamespacesFromFields($data, $element->ID);

// create a temporary Form to use for validation - will contain existing dataobject values
$form = $this->getElementForm($id);
// remove element namespaces from fields so that something like RequiredFields('Title') works
// element namespaces are added in DNADesign\Elemental\Forms\EditFormFactory
foreach ($form->Fields()->flattenFields() as $field) {
$rx = '#^PageElements_[0-9]+_#';
$namespacedName = $field->getName();
if (!preg_match($rx, $namespacedName)) {
continue;
}
$regularName = preg_replace($rx, '', $namespacedName);
// If there's an existing field with the same name, remove it
// this is probably a workaround for EditFormFactory creating too many fields?
// e.g. for element #2 there's a "Title" field and a "PageElements_2_Title" field
// same with "SecurityID" and "PageElements_2_SecurityID"
// possibly this would be better to just remove fields if they match the rx, not sure,
// this approach seems more conservative
if ($form->Fields()->flattenFields()->fieldByName($regularName)) {
$form->Fields()->removeByName($regularName);
}
// update the name of the field
$field->setName($regularName);
}
// merge submitted data into the form
$form->loadDataFrom($data);
// Validate the Form
/** @var ValidationResult|null $validationResult */
$validationResult = $form->getValidator()?->validate();
if ($validationResult && !$validationResult->isValid()) {
$this->jsonError(400, $validationResult->getMessages());
return null;
}

try {
$updated = false;

Expand Down

0 comments on commit 595c436

Please sign in to comment.