From a40a8038496aaec37ebec84f8b173bb04e511e76 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Wed, 4 Mar 2015 22:15:48 +0000
Subject: [PATCH] forms: Bubble up errors set on field to the form

Add ability to mass set errors
---
 include/class.forms.php | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/include/class.forms.php b/include/class.forms.php
index 252fe84cf..e3791044b 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -144,8 +144,21 @@ class Form {
         return ($formOnly) ? $this->_errors['form'] : $this->_errors;
     }
 
-    function addError($message) {
-        $this->_errors['form'][] = $message;
+    function addError($message, $index=false) {
+
+        if ($index)
+            $this->_errors[$index] = $message;
+        else
+            $this->_errors['form'][] = $message;
+    }
+
+    function addErrors($errors=array()) {
+        foreach ($errors as $k => $v) {
+            if (($f=$this->getField($k)))
+                $f->addError($v);
+            else
+                $this->addError($v, $k);
+        }
     }
 
     function addValidator($function) {
@@ -429,11 +442,15 @@ class FormField {
     function errors() {
         return $this->_errors;
     }
-    function addError($message, $field=false) {
+    function addError($message, $index=false) {
         if ($field)
-            $this->_errors[$field] = $message;
+            $this->_errors[$index] = $message;
         else
             $this->_errors[] = $message;
+
+        // Update parent form errors for the field
+        if ($this->_form)
+            $this->_form->addError($this->errors(), $this->get('id'));
     }
 
     function isValidEntry() {
-- 
GitLab