mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-20 11:26:13 +00:00
cleanup ApiMutableModelControllerBase, remove duplicate code and make sure all validation errors are returned to the client. (in stead of only the first)
This commit is contained in:
parent
829c0a8f39
commit
9bde30e6cb
@ -151,9 +151,17 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
|
||||
// replace absolute path to attribute for relative one at uuid.
|
||||
if ($node != null) {
|
||||
$fieldnm = str_replace($node->__reference, $resultPrefix, $msg->getField());
|
||||
$result["validations"][$fieldnm] = $msg->getMessage();
|
||||
} else {
|
||||
$result["validations"][$resultPrefix.".".$msg->getField()] = $msg->getMessage();
|
||||
$fieldnm = $resultPrefix.".".$msg->getField();
|
||||
}
|
||||
if (empty($result["validations"][$fieldnm])) {
|
||||
$result["validations"][$fieldnm] = $msg->getMessage();
|
||||
} elseif (!is_array($result["validations"][$fieldnm])) {
|
||||
// multiple validations, switch to array type output
|
||||
$result["validations"][$fieldnm] = array($result["validations"][$fieldnm]);
|
||||
$result["validations"][$fieldnm][] = $msg->getMessage();
|
||||
} else {
|
||||
$result["validations"][$fieldnm][] = $msg->getMessage();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
@ -271,7 +279,6 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
|
||||
{
|
||||
$result = array("result" => "failed");
|
||||
if ($this->request->isPost() && $this->request->hasPost($post_field)) {
|
||||
$result = array("result" => "failed", "validations" => array());
|
||||
$mdl = $this->getModel();
|
||||
$tmp = $mdl;
|
||||
foreach (explode('.', $path) as $step) {
|
||||
@ -279,19 +286,15 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
|
||||
}
|
||||
$node = $tmp->Add();
|
||||
$node->setNodes($this->request->getPost($post_field));
|
||||
$valMsgs = $mdl->performValidation();
|
||||
$result = $this->validate($node, $post_field);
|
||||
|
||||
foreach ($valMsgs as $field => $msg) {
|
||||
$fieldnm = str_replace($node->__reference, $post_field, $msg->getField());
|
||||
$result["validations"][$fieldnm] = $msg->getMessage();
|
||||
}
|
||||
|
||||
if (count($result['validations']) == 0) {
|
||||
if (empty($result['validations'])) {
|
||||
// save config if validated correctly
|
||||
$mdl->serializeToConfig();
|
||||
Config::getInstance()->save();
|
||||
unset($result['validations']);
|
||||
$result["result"] = "saved";
|
||||
$result = array("result" => "saved");
|
||||
} else {
|
||||
$result["result"] = "failed";
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
@ -345,20 +348,15 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
|
||||
if ($uuid != null) {
|
||||
$node = $mdl->getNodeByReference($path . '.' . $uuid);
|
||||
if ($node != null) {
|
||||
$result = array("result" => "failed", "validations" => array());
|
||||
|
||||
$node->setNodes($this->request->getPost($post_field));
|
||||
$valMsgs = $mdl->performValidation();
|
||||
foreach ($valMsgs as $field => $msg) {
|
||||
$fieldnm = str_replace($node->__reference, $post_field, $msg->getField());
|
||||
$result["validations"][$fieldnm] = $msg->getMessage();
|
||||
}
|
||||
|
||||
if (count($result['validations']) == 0) {
|
||||
$result = $this->validate($node, $post_field);
|
||||
if (empty($result['validations'])) {
|
||||
// save config if validated correctly
|
||||
$mdl->serializeToConfig();
|
||||
Config::getInstance()->save();
|
||||
$result = array("result" => "saved");
|
||||
} else {
|
||||
$result["result"] = "failed";
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user