summaryrefslogtreecommitdiff
path: root/includes/form/Form.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/form/Form.php')
-rw-r--r--includes/form/Form.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/includes/form/Form.php b/includes/form/Form.php
new file mode 100644
index 0000000..3f1bd62
--- /dev/null
+++ b/includes/form/Form.php
@@ -0,0 +1,45 @@
+<?php
+
+abstract class Form
+{
+ private $errors = array();
+ private $success = true;
+
+ protected function report_error($error_string)
+ {
+ $this->errors[] = $error_string;
+ $this->success = false;
+ }
+
+ public function success(): bool
+ {
+ return $this->success;
+ }
+
+ public function html_error_list(): string
+ {
+ if ($this->success)
+ return "";
+
+ if (count($this->errors) > 1) {
+ $result = '<ul>';
+ foreach ($this->errors as $err) {
+ $result .= '<li>' . $err . '</li>';
+ }
+ $result .= '</ul>';
+ return $result;
+ } else {
+ return $this->errors[0];
+ }
+ }
+
+ public function on_success(Closure $param)
+ {
+ if ($this->success()) {
+ $param();
+ } else {
+ echo '<p>Please check the following problems:</p>';
+ trigger_error($this->html_error_list());
+ }
+ }
+} \ No newline at end of file