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; }