mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-20 03:16:12 +00:00
extended example with a simple REST call
This commit is contained in:
parent
1925af934f
commit
c60d3f3e46
@ -41,6 +41,12 @@ class IndexController extends ApiControllerBase
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param array $error_msg error messages
|
||||
@ -102,8 +128,11 @@ class PageController extends ControllerBase
|
||||
$this->updateModelWithPost($mdlSample);
|
||||
|
||||
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();
|
||||
if ($this->performFormValidation($mdlSample) == false) {
|
||||
return false;
|
||||
}
|
||||
$mdlSample->serializeToConfig();
|
||||
} elseif ($this->request->getPost("form_action") == "save") {
|
||||
// implement save, possible removing
|
||||
@ -120,23 +149,12 @@ class PageController extends ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
// save data to config
|
||||
$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)
|
||||
));
|
||||
// form validation
|
||||
if ($this->performFormValidation($mdlSample) == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// save data to config
|
||||
$mdlSample->serializeToConfig();
|
||||
$cnf = Config::getInstance();
|
||||
$cnf->save();
|
||||
|
||||
@ -1,3 +1,36 @@
|
||||
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>
|
||||
|
||||
|
||||
@ -1 +0,0 @@
|
||||
//////footer/////
|
||||
@ -2,6 +2,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>{{title|default("OPNsense") }}</title>
|
||||
<script type="text/javascript" src="/js/jquery-1.11.1.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
{{ content() }}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user