Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
d48d4191
Commit
d48d4191
authored
Aug 09, 2016
by
Per von Zweigbergk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(Base) Refactor to split out saving/validation logic
parent
05194786
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
16 deletions
+33
-16
ApiMutableModelControllerBase.php
...ntrollers/OPNsense/Base/ApiMutableModelControllerBase.php
+33
-16
No files found.
src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php
View file @
d48d4191
...
...
@@ -107,6 +107,38 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
return
$this
->
modelHandle
;
}
/**
* validate and save model after update or insertion.
* 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
*/
protected
function
save
(
$node
=
null
)
{
$mdl
=
$this
->
getModel
();
$result
=
array
(
"result"
=>
"failed"
,
"validations"
=>
array
());
// perform validation
$valMsgs
=
$mdl
->
performValidation
();
foreach
(
$valMsgs
as
$field
=>
$msg
)
{
// replace absolute path to attribute for relative one at uuid.
if
(
$node
!=
null
)
{
$fieldnm
=
str_replace
(
$node
->
__reference
,
static
:
$internalModelName
,
$msg
->
getField
());
$result
[
"validations"
][
$fieldnm
]
=
$msg
->
getMessage
();
}
else
{
$result
[
"validations"
][
$msg
->
getField
()]
=
$msg
->
getMessage
();
}
}
// serialize model to config and save when there are no validation errors
if
(
count
(
$result
[
'validations'
])
==
0
)
{
// save config if validated correctly
$mdl
->
serializeToConfig
();
Config
::
getInstance
()
->
save
();
$result
=
array
(
"result"
=>
"saved"
);
}
return
$result
;
}
/**
* update model settings
* @return array status / validation errors
...
...
@@ -118,22 +150,7 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
// load model and update with provided data
$mdl
=
$this
->
getModel
();
$mdl
->
setNodes
(
$this
->
request
->
getPost
(
static
::
$internalModelName
));
// perform validation
$valMsgs
=
$mdl
->
performValidation
();
foreach
(
$valMsgs
as
$field
=>
$msg
)
{
if
(
!
array_key_exists
(
"validations"
,
$result
))
{
$result
[
"validations"
]
=
array
();
}
$result
[
"validations"
][
static
::
$internalModelName
.
"."
.
$msg
->
getField
()]
=
$msg
->
getMessage
();
}
// serialize model to config and save
if
(
$valMsgs
->
count
()
==
0
)
{
$mdl
->
serializeToConfig
();
Config
::
getInstance
()
->
save
();
$result
[
"result"
]
=
"saved"
;
}
$result
=
$this
->
save
();
}
return
$result
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment