-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.class.php
More file actions
112 lines (89 loc) · 2.85 KB
/
Form.class.php
File metadata and controls
112 lines (89 loc) · 2.85 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
*
*/
class Ashtree_Form {
protected $dom;
protected $form = array();
public $name;
public $method = 'post';
public $action = '';
public $enctype = '';
public $format = 'standard';
public $semicolons = false;
public $autocomplete = true;
public function __construct($parent=NULL) {
$this->dom = isset($parent) ? $parent : new DOMDocument();
$this->name = str_replace(array('-', '.'), '_', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_FILENAME));
if (is_numeric($this->name)) $this->name = "item{$this->name}";
$htm = Ashtree_Html_Page::instance();
$htm->jss = ASH_LIB . 'Ashtree/Javascript/jquery.validate.min.js';
$htm->jquery = "
var {$this->name}_rules = {};
var {$this->name}_msgs = {};
//New rule for select boxes
$.validator.addMethod('notEqualTo', function(value, element, arg) {
return arg.toLowerCase() != value.toLowerCase();
}, 'Value can not be the same');
";
}
public function __destruct() {
$htm = Ashtree_Html_Page::instance();
$htm->jquery = "
$('form').validate({
rules: {$this->name}_rules,
messages: {$this->name}_msgs,
submitHandler: function(form) {
$('input[type=submit]').attr('disabled', true);
form.submit();
}
});
";
}
public function createFieldset($title) {
return new Widget_Fieldset($this->dom);
}
public function createField($field_type) {
$Widget = "Ashtree_Form_Widget_" . ucfirst($field_type);
return new $Widget($this->dom);
}
public function createControl($control_type) {
$Widget = "Ashtree_Form_Widget_" . ucfirst($control_type);
return new $Widget($this->dom);
}
public function createEmail(){
return new Ashtree_Form_Email();
}
public function addFieldset($fieldset) {
$this->form['field'][$fieldset->legend] = $field;
}
public function addField($field) {
//$this->form['field'][$field->name] = $field;
$this->form['field'][] = $field;
}
public function addControl($control) {
$this->form['control'][] = $control;
}
public function addEmail() {
}
public function __invoke($print=TRUE) {
$dom = $this->dom;
$dom->formatOutput = true;
$frm = $dom->createElement('form');
if ($this->name) $frm->setAttribute('id', $this->name);
if ($this->method) $frm->setAttribute('method', $this->method);
if ($this->action) $frm->setAttribute('action', $this->action);
if ($this->enctype) $frm->setAttribute('enctype', $this->enctype);
if (!$this->autocomplete) $frm->setAttribute('autocomplete', 'off');
foreach($this->form['field'] as $field) {
$frm->appendChild($field(FALSE));
}
$ctl = $dom->createElement('div');
$ctl->setAttribute('class', 'form-controls');
foreach((array)$this->form['control'] as $control) {
$ctl->appendChild($control(FALSE));
}
$frm->appendChild($ctl);
return $print ? $dom->saveXML($frm) : $frm;
}
}