-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.php
67 lines (53 loc) · 1.48 KB
/
Form.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
class Form
{
public static function begin($params = []) {
$attrs = '';//" method=\"{$method}\"";
if ($params) {
foreach($params as $attr => $value) {
if (!empty($value)) $attrs .= " {$attr}='{$value}'";
}
}
echo "<form{$attrs}>";
}
public static function end() {
echo '</form>';
}
public static function input($name, $params = []) {
if (empty($name)) return;
$label = '';
$attrs = '';//" type=\"{$type}\"";
$label_after = false;
if ($name == 'token') {
echo "<input name='{$name}' type='hidden' value='" . Token::generate() . "'>";
return;
}
$params['name'] = $name;
if (!array_key_exists('type', $params)) $params['type'] = 'text';
if (!array_key_exists('id', $params)) $params['id'] = $name;
if (array_key_exists('label', $params)) {
$label = $params['label'];
unset($params['label']);
$label = "<label for=\"{$params['id']}\">{$label}</label>";
if (array_key_exists('label_after', $params)) {
$label_after = $params['label_after'];
unset($params['label_after']);
}
}
if ($params) {
foreach($params as $attr => $value) {
if (!empty($value)) $attrs .= " {$attr}='{$value}'";
}
}
echo $label_after ? "<input{$attrs}> {$label}" : "{$label} <input{$attrs}>";
}
public static function button($params = [], $text = '') {
$attrs = '';
if ($params) {
foreach($params as $attr => $value) {
if (!empty($value)) $attrs .= " {$attr}='{$value}'";
}
}
echo "<button{$attrs}>{$text}</button>";
}
}