From 3c98dd7c39da2a58902b4e2d2545b5018a2d2bc2 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 15 Apr 2016 15:08:02 +0000 Subject: [PATCH] form : add the methods getOldValues, getOldErrors, saveValuesErrors and saveValuesErrorsReset to simplify the codes. In views : $form = new \form (); $values = array (); $errors = array (); // If there is saved values, use them, else return the provided $values $values = $form->getOldValues ($values); $errors = $form->getOldErrors ($errors); In index.php : $form = new \form (); $values = $form->values (); $errors = $ipsetsObj->verify ($values); $form->saveValuesErrors ($values, $errors); if (count ($errors)) $route->redirect ("/ipsets/add"); $ipsetsObj->createSet ($values["setname"], $values["typename"]); // If there is no error (catched by exception), clear the form for next // time $form->saveValuesErrorsReset (); $route->redirect ("/ipsets", ""); git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2697 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- form.php | 62 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/form.php b/form.php index e5a7815..fadb0d8 100644 --- a/form.php +++ b/form.php @@ -132,17 +132,6 @@ class form die ("FORM/VERIFY : UNUSED and deprecated\n"); } - /** Save the values and errors to be displayed in the next page if the session - is available */ - public function saveValuesErrors ($values, $errors) - { - if (isset ($_SESSION)) - { - $_SESSION["domframework"]["form"]["values"] = $values; - $_SESSION["domframework"]["form"]["errors"] = $errors; - } - } - /** Return the fields in HTML code. If $values is provided, use it in place of default values. In case of select boxes, $values are the selected elements @@ -239,6 +228,57 @@ class form $csrf->field = $this->csrfField; $csrf->checkToken ($tokenFromUser); } + + /** Save the values and errors to be displayed in the next page if the session + * is available + * @param array $values The values of the fields filled by the user + * @param array|null $errors The errors detected by a verify + */ + public function saveValuesErrors ($values, $errors=array ()) + { + if (isset ($_SESSION)) + { + $_SESSION["domframework"]["form"]["values"] = $values; + $_SESSION["domframework"]["form"]["errors"] = $errors; + } + } + + /** Reset the saved values to provide a clean form next page */ + public function saveValuesErrorsReset () + { + unset ($_SESSION["domframework"]["form"]["values"]); + unset ($_SESSION["domframework"]["form"]["errors"]); + } + + /** Get the stored values if there is one. If there is no stored values, + * return the values provided as parameter + * @param array $values The values returned if there is no stored values + * @return array The values to use + */ + public function getOldValues ($values) + { + if (isset ($_SESSION["domframework"]["form"]["values"])) + { + $values = $_SESSION["domframework"]["form"]["values"]; + unset ($_SESSION["domframework"]["form"]["values"]); + } + return $values; + } + + /** Get the stored errors if there is one. If there is no sorted errors, + * return the errors provided as parameter + * @param array $errors The values returned if there is no stored values + * @return array The errors to use + */ + public function getOldErrors ($errors) + { + if (isset ($_SESSION["domframework"]["form"]["errors"])) + { + $errors = $_SESSION["domframework"]["form"]["errors"]; + unset ($_SESSION["domframework"]["form"]["errors"]); + } + return $errors; + } } /** the definition of a formfield */