From 0d63ce1e341596ea27d285cf877bc2cd12b94836 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 2 May 2017 14:17:46 +0000 Subject: [PATCH] form : If the error is not due to field (it has numerical key, put the focus on the first field (if available) and not on the global error git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3562 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- form.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/form.php b/form.php index db5efcd..468ab00 100644 --- a/form.php +++ b/form.php @@ -221,19 +221,33 @@ class form // Manage the focus. On the first visible element if there is no error, on // the first error fields when there is one + $focusElement = null; foreach ($this->fields as $field) { - if ($field->type !== "hidden" && $field->readonly !== true) - break; + if ($field->type === "hidden" || $field->readonly === true) + continue; + $focusElement = $field->name; + break; } - $focusElement = $field->name; if (count ($errors) > 0) { - reset ($errors); - $focusElement = key ($errors); + foreach ($errors as $fieldErr=>$error) + { + // If the field is numeric, it is a global error, and not an error due + // to a field: skip it ! + foreach ($this->fields as $field) + { + if ($field->name === $fieldErr) + { + $focusElement = $field->name; + break 2; + } + } + } } - $res .= "\n"; + if ($focusElement !== null) + $res .= "\n"; $res .= "\n"; return $res; }