Commit d48d4191 authored by Per von Zweigbergk's avatar Per von Zweigbergk

(Base) Refactor to split out saving/validation logic

parent 05194786
...@@ -108,32 +108,49 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase ...@@ -108,32 +108,49 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
} }
/** /**
* update model settings * validate and save model after update or insertion.
* @return array status / validation errors * Use the reference node and tag to rename validation output for a specific node to a new offset, which makes
* it easier to reference specific uuids without having to use them in the frontend descriptions.
* @param $node reference node, to use as relative offset
* @return array result / validation output
*/ */
public function setAction() protected function save($node = null)
{ {
$result = array("result"=>"failed");
if ($this->request->isPost()) {
// load model and update with provided data
$mdl = $this->getModel(); $mdl = $this->getModel();
$mdl->setNodes($this->request->getPost(static::$internalModelName)); $result = array("result"=>"failed","validations" => array());
// perform validation // perform validation
$valMsgs = $mdl->performValidation(); $valMsgs = $mdl->performValidation();
foreach ($valMsgs as $field => $msg) { foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) { // replace absolute path to attribute for relative one at uuid.
$result["validations"] = array(); if ($node != null) {
$fieldnm = str_replace($node->__reference, static:$internalModelName, $msg->getField());
$result["validations"][$fieldnm] = $msg->getMessage();
} else {
$result["validations"][$msg->getField()] = $msg->getMessage();
} }
$result["validations"][static::$internalModelName.".".$msg->getField()] = $msg->getMessage();
} }
// serialize model to config and save when there are no validation errors
// serialize model to config and save if (count($result['validations']) == 0) {
if ($valMsgs->count() == 0) { // save config if validated correctly
$mdl->serializeToConfig(); $mdl->serializeToConfig();
Config::getInstance()->save(); Config::getInstance()->save();
$result["result"] = "saved"; $result = array("result" => "saved");
} }
return $result;
}
/**
* update model settings
* @return array status / validation errors
*/
public function setAction()
{
$result = array("result"=>"failed");
if ($this->request->isPost()) {
// load model and update with provided data
$mdl = $this->getModel();
$mdl->setNodes($this->request->getPost(static::$internalModelName));
$result = $this->save();
} }
return $result; return $result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment