From 191b795627bdf4836f6422e738fdb1485896d67a Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 26 Apr 2016 12:27:00 +0000 Subject: [PATCH] form : add the verify of forms. Actually check only the mandatory part git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2705 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- form.php | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/form.php b/form.php index 50d36b7..c2273e2 100644 --- a/form.php +++ b/form.php @@ -118,20 +118,6 @@ class form return $values; } - /** Return TRUE if the value associated to a field is correct. Return an - array with a severity and a message to explain why a field is not - correct. - Fields can be an array with just one element, then only this element is - checked - @param array $fieldsVerify The fields to verify - @param array|null $valuesVerify The values of the fields to verify - @deprecated 0.17 - */ - public function verify (&$fieldsVerify, $valuesVerify = NULL) - { - die ("FORM/VERIFY : UNUSED and deprecated\n"); - } - /** 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 @@ -222,6 +208,8 @@ class form $res .= "\n"; $res .= "\n"; + if (isset ($_SESSION)) + $_SESSION["domframework"]["form"]["fields"] = $this->fields; return $res; } @@ -234,8 +222,32 @@ class form $csrf->checkToken ($tokenFromUser); } + /** Check if the parameters are correct with the defined fields + * Need the session ! + * @return array containing the errors + */ + public function verify ($values, $fields=array ()) + { + if (count ($fields) === 0) + { + if (! isset ($_SESSION["domframework"]["form"]["fields"])) + return array (); + $fields = $_SESSION["domframework"]["form"]["fields"]; + } + $errors = array (); + foreach ($fields as $field) + { + if ($field->mandatory !== null && + (! array_key_exists ($field->name, $values) || + trim ($values[$field->name]) === "")) + $errors[$field->name] = _("Field mandatory and not provided"); + } + return $errors; + } + /** Save the values and errors to be displayed in the next page if the session * is available + * Need the session to work * @param array $values The values of the fields filled by the user * @param array|null $errors The errors detected by a verify */ @@ -248,7 +260,9 @@ class form } } - /** Reset the saved values to provide a clean form next page */ + /** Reset the saved values to provide a clean form next page + * Need the session to work + */ public function saveValuesErrorsReset () { unset ($_SESSION["domframework"]["form"]["values"]);