From c60d3f3e4690ca8234aa529724de6bcbc2726bdb Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 6 Feb 2015 18:11:29 +0100 Subject: [PATCH] extended example with a simple REST call --- .../OPNsense/Sample/Api/IndexController.php | 8 +++- .../OPNsense/Sample/PageController.php | 48 +++++++++++++------ .../mvc/app/views/OPNsense/Sample/index.volt | 39 +++++++++++++-- .../mvc/app/views/layout_partials/footer.volt | 1 - .../mvc/app/views/layouts/default.volt | 1 + 5 files changed, 77 insertions(+), 20 deletions(-) delete mode 100644 src/opnsense/mvc/app/views/layout_partials/footer.volt diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Sample/Api/IndexController.php b/src/opnsense/mvc/app/controllers/OPNsense/Sample/Api/IndexController.php index 5d3dbe7da..61f8fa52d 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Sample/Api/IndexController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Sample/Api/IndexController.php @@ -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); } } diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php b/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php index 09a07d4d7..6e52a7522 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php @@ -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:///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(); diff --git a/src/opnsense/mvc/app/views/OPNsense/Sample/index.volt b/src/opnsense/mvc/app/views/OPNsense/Sample/index.volt index a429ce503..49eae8c96 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Sample/index.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Sample/index.volt @@ -1,3 +1,36 @@ -test sample index page -{{ title }} -{{ partial('layout_partials/footer') }} + + +

OPNsense sample module

+ +A simple input form for the "sample" model can be found here
+ +
+ +To perform a call to the api, press this button :
+ +fill in a message :
+ + + + +
+ +API call result :
+ + + + diff --git a/src/opnsense/mvc/app/views/layout_partials/footer.volt b/src/opnsense/mvc/app/views/layout_partials/footer.volt deleted file mode 100644 index b230afc5e..000000000 --- a/src/opnsense/mvc/app/views/layout_partials/footer.volt +++ /dev/null @@ -1 +0,0 @@ -//////footer///// diff --git a/src/opnsense/mvc/app/views/layouts/default.volt b/src/opnsense/mvc/app/views/layouts/default.volt index 89b20def6..144ddfdf2 100644 --- a/src/opnsense/mvc/app/views/layouts/default.volt +++ b/src/opnsense/mvc/app/views/layouts/default.volt @@ -2,6 +2,7 @@ {{title|default("OPNsense") }} + {{ content() }}