/
Form.class.php
94 lines (80 loc) · 2.22 KB
/
Form.class.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
Library::import('recess.framework.forms.FormInput');
Library::import('recess.framework.forms.TextAreaInput');
Library::import('recess.framework.forms.DateTimeInput');
Library::import('recess.framework.forms.TextInput');
Library::import('recess.framework.forms.LabelInput');
Library::import('recess.framework.forms.DateLabelInput');
Library::import('recess.framework.forms.BooleanInput');
Library::import('recess.framework.forms.HiddenInput');
class Form {
protected $name;
public $method;
public $action;
public $flash;
function __construct($name) {
$this->name = $name;
}
public $hasErrors = false;
public $inputs = array();
function __get($name) {
if(isset($this->inputs[$name])) {
return $this->inputs[$name];
} else {
return '';
}
}
// function __set($name, $value) {
// if(isset($this->inputs[$name])) {
// $this->inputs[$name]->value = $value;
// }
// }
function to($method, $action) {
$this->method = $method;
$this->action = $action;
}
function begin() {
if($this->method == Methods::DELETE || $this->method == Methods::PUT) {
echo '<form method="POST" action="' . $this->action . '">';
echo '<input type="hidden" name="_METHOD" value="' . $this->method . '" />';
} else {
echo '<form method="' . $this->method . '" action="' . $this->action . '">';
}
}
function input($name, $class = '') {
if($class != '') {
$this->inputs[$name]->class = $class;
}
$this->inputs[$name]->render();
}
function changeInput($name, $newInput) {
$current = $this->inputs[$name];
$newInput .= 'Input';
$this->inputs[$name] = new $newInput($name);
$this->inputs[$name]->setValue($current->getValue());
}
function fill(array $keyValues) {
foreach($this->inputs as $key => $value) {
if(isset($keyValues[$key])) {
$this->inputs[$key]->setValue($keyValues[$key]);
}
}
}
function assertNotEmpty($inputName) {
if(isset($this->inputs[$inputName]) && $this->inputs[$inputName]->getValue() != '') {
return true;
} else {
$this->inputs[$inputName]->class = 'highlight';
$this->inputs[$inputName]->flash = 'Required.';
$this->hasErrors = true;
return false;
}
}
function hasErrors() {
return $this->hasErrors;
}
function end() {
echo '</form>';
}
}
?>