From: Andrey Kutejko Date: Sun, 5 Apr 2015 07:05:14 +0000 (+0300) Subject: encapsulate form errors X-Git-Tag: 0.6~54 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=ff3ab2d9acc4a063ffef99ced408e15828dadb27;p=ipf.git encapsulate form errors --- diff --git a/ipf/auth/admin.php b/ipf/auth/admin.php index ed9024b..9a6df25 100644 --- a/ipf/auth/admin.php +++ b/ipf/auth/admin.php @@ -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; diff --git a/ipf/form/boundfield.php b/ipf/form/boundfield.php index 41defe2..fcff536 100644 --- a/ipf/form/boundfield.php +++ b/ipf/form/boundfield.php @@ -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() diff --git a/ipf/form/form.php b/ipf/form/form.php index e5f2d3b..315ba30 100644 --- a/ipf/form/form.php +++ b/ipf/form/form.php @@ -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() diff --git a/ipf/form/layout.php b/ipf/form/layout.php index 5387a20..536eee2 100644 --- a/ipf/form/layout.php +++ b/ipf/form/layout.php @@ -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; }