Commit c60d3f3e authored by Ad Schellevis's avatar Ad Schellevis

extended example with a simple REST call

parent 1925af93
...@@ -41,6 +41,12 @@ class IndexController extends ApiControllerBase ...@@ -41,6 +41,12 @@ class IndexController extends ApiControllerBase
*/ */
public function indexAction() public function indexAction()
{ {
return array("message" => "test"); if ($this->request->hasPut("message")) {
$message = $this->request->getPut("message");
} else {
$message = " " ;
}
return array("message" => "you send : ". $message);
} }
} }
...@@ -64,6 +64,32 @@ class PageController extends ControllerBase ...@@ -64,6 +64,32 @@ class PageController extends ControllerBase
} }
} }
/**
* perform validation, forward to index on failure.
* @param $mdlSample Sample model
* @return bool validation status
*/
private function performFormValidation($mdlSample)
{
$validationOutput = $mdlSample->performValidation();
if ($validationOutput->count() > 0) {
// forward to index including errors
$error_msgs = array();
foreach ($validationOutput as $msg) {
$error_msgs[] = array("field" => $msg-> getField(), "msg" => $msg->getMessage());
}
// redirect to index
$this->dispatcher->forward(array(
"action" => "index",
"params" => array($error_msgs)
));
return false;
}
return true;
}
/** /**
* controller for sample index page, defaults to http://<host>/sample/page * controller for sample index page, defaults to http://<host>/sample/page
* @param array $error_msg error messages * @param array $error_msg error messages
...@@ -102,8 +128,11 @@ class PageController extends ControllerBase ...@@ -102,8 +128,11 @@ class PageController extends ControllerBase
$this->updateModelWithPost($mdlSample); $this->updateModelWithPost($mdlSample);
if ($this->request->getPost("form_action") == "add") { if ($this->request->getPost("form_action") == "add") {
// implement addRow, append new model row and serialize to config // implement addRow, append new model row and serialize to config if form is valid
$mdlSample->childnodes->section->add(); $mdlSample->childnodes->section->add();
if ($this->performFormValidation($mdlSample) == false) {
return false;
}
$mdlSample->serializeToConfig(); $mdlSample->serializeToConfig();
} elseif ($this->request->getPost("form_action") == "save") { } elseif ($this->request->getPost("form_action") == "save") {
// implement save, possible removing // implement save, possible removing
...@@ -120,23 +149,12 @@ class PageController extends ControllerBase ...@@ -120,23 +149,12 @@ class PageController extends ControllerBase
} }
} }
// save data to config // form validation
$validationOutput = $mdlSample->performValidation(); if ($this->performFormValidation($mdlSample) == false) {
if ($validationOutput->count() > 0) {
// forward to index including errors
$error_msgs = array();
foreach ($validationOutput as $msg) {
$error_msgs[] = array("field" => $msg-> getField(), "msg" => $msg->getMessage());
}
// redirect to index
$this->dispatcher->forward(array(
"action" => "index",
"params" => array($error_msgs)
));
return false; return false;
} }
// save data to config
$mdlSample->serializeToConfig(); $mdlSample->serializeToConfig();
$cnf = Config::getInstance(); $cnf = Config::getInstance();
$cnf->save(); $cnf->save();
......
test sample index page
{{ title }}
{{ partial('layout_partials/footer') }} <h1> OPNsense sample module </h1>
A simple input form for the "sample" model can be found <a href="page/">here </a><br/>
<hr/>
To perform a call to the api, press this button : <br/>
fill in a message : <input type="text" value="" id="msg"> </br>
<input type="button" id="restcall" value="do REST call!"/>
<br/>
API call result : <div id="msgid"></div>
<script type="text/javascript">
$( "#restcall" ).click( function() {
$.ajax({
type: "PUT",
url: "/api/sample/",
success: function(data){
$("#msgid").html( data.message );
},
data:{message:$("#msg").val()}
});
});
</script>
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<html> <html>
<head> <head>
<title>{{title|default("OPNsense") }}</title> <title>{{title|default("OPNsense") }}</title>
<script type="text/javascript" src="/js/jquery-1.11.1.min.js"></script>
</head> </head>
<body> <body>
{{ content() }} {{ content() }}
......
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