summaryrefslogtreecommitdiff
path: root/includes/form/Form.php
blob: 3f1bd625c5999dc6531ec0dc6916a69e9e94eae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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());
		}
	}
}