]> git.andy128k.dev Git - ipf.git/commitdiff
encapsulate form errors
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 5 Apr 2015 07:05:14 +0000 (10:05 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 6 Apr 2015 17:05:58 +0000 (20:05 +0300)
ipf/auth/admin.php
ipf/form/boundfield.php
ipf/form/form.php
ipf/form/layout.php

index ed9024bdb6f3faebc19013e83e07d51183c050f4..9a6df250822f7d2d6153ac55d78118a5a68f9ccc 100644 (file)
@@ -124,8 +124,8 @@ class UserForm extends \IPF_ObjectForm
         $data = parent::clean();
 
         if ($this->isAdd || $data['password1'] || $data['password2']) {
-            if ($data['password1'] != $data['password2'])
-                $this->errors['password2'][] = __('The two password fields didn\'t match.');
+            if ($data['password1'] !== $data['password2'])
+                $this->addError('password1', __('The two password fields didn\'t match.'));
         }
 
         return $data;
index 41defe2a654212e791234d163d404219db922bbf..fcff53666b9df8d9c23d9423c6edcb69de5de816 100644 (file)
@@ -10,7 +10,7 @@ class IPF_Form_BoundField
     public $html_name = null;
     public $label = null;
     public $help_text = null;
-    public $errors = array();
+    public $errors;
 
     public function __construct($form, $field, $name)
     {
@@ -29,9 +29,7 @@ class IPF_Form_BoundField
         }
 
         $this->help_text = ($this->field->help_text) ? $this->field->help_text : '';
-        if (isset($this->form->errors[$name])) {
-            $this->errors = $this->form->errors[$name];
-        }
+        $this->errors = $this->form->errors($name);
     }
 
     public function value()
index e5f2d3b68c3187055a7d6bcacbfff3b358c53fa9..315ba30ec494719f416787cb37c3877cf9108ef9 100644 (file)
@@ -12,7 +12,7 @@ abstract class IPF_Form implements Iterator
     public $initial = null;
     public $data = array();
     public $cleaned_data = array();
-    public $errors = array();
+    private $errors = array();
     public $is_bound = false;
     public $label_suffix = ':';
 
@@ -90,6 +90,13 @@ abstract class IPF_Form implements Iterator
     {
         if (!$field)
             $field = '__all__';
+
+        if (!($error instanceof IPF_Form_Error)) {
+            if ($error instanceof \Exception)
+                $error = $this->getMessage();
+            $error = new IPF_Form_Error((string)$error);
+        }
+
         \PFF\Arr::pushToKey($this->errors, $field, $error);
     }
 
@@ -112,9 +119,11 @@ abstract class IPF_Form implements Iterator
         return $hidden;
     }
 
-    public function errors()
+    public function errors($field=null)
     {
-        return \PFF\Arr::get($this->errors, '__all__', array());
+        if (!$field)
+            $field = '__all__';
+        return \PFF\Arr::get($this->errors, $field, array());
     }
 
     public function renderErrors()
index 5387a20666df53874ce9ee7e2294224c35ab0271..536eee21e75491e322783f641f4df250769ddbce 100644 (file)
@@ -24,7 +24,7 @@ abstract class IPF_Form_LayoutAdapter /*implements informal interface IPF_Form_L
     {
         $commonErrors = $form->errors();
         foreach ($form->hiddenFields() as $field_name)
-            foreach (\PFF\Arr::get($form->errors, $field_name, array()) as $error)
+            foreach ($form->errors($field_name) as $error)
                 $commonErrors[] = sprintf(__('(Hidden field %1$s) %2$s'), $field_name, $error);
         return $commonErrors;
     }