-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm.php
More file actions
executable file
·403 lines (343 loc) · 10.4 KB
/
Form.php
File metadata and controls
executable file
·403 lines (343 loc) · 10.4 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* Form.php
*
* This file contains the implementation of the WebLab_Form class.
* @see WebLab_Form
*/
/**
* Provides form abstraction, integrating retrieval and validation into one package.
*
* @author Jorgen Evens <jorgen@wlab.be>
* @package WebLab
*
*/
abstract class WebLab_Form
{
/**
* Constant for POST method
*
* @var string
*/
const POST = 'POST';
/**
* Constant for GET method
*
* @var string
*/
const GET = 'GET';
/**
* Fields that are part of this form.
*
* @var array
*/
private $_fields = array();
/**
* Method being used by the form
*
* @var string
*/
protected $_method = self::POST;
/**
* Action of the form
*
* @var string
*/
protected $_action = '?';
/**
* Validation errors that have occured.
*
* @var array
*/
private $_errors = array();
/**
* Postback detection element
*
* @var WebLab_Form_Input
*/
public $postback = null;
/**
* Construct new form
*
* @param string $action
* @param string $method
*/
public function __construct( $action='', $method = self::POST ){
$this->_action = $action;
$this->_method = $method;
$this->_setupFields();
$this->_setupPostback();
if( $this->isPostback() ) {
$this->update();
$this->_setupValidation();
}
}
/**
* Declare fields used by this form.
*
*/
protected function _setupFields(){}
/**
* Declare validation rules for this form.
*
*/
protected function _setupValidation(){}
/**
* Generate postback element
*
*/
protected function _setupPostback(){
$this->postback = new WebLab_Form_Input( $this->getFormId(), 'hidden', null, 'postback' );
}
/**
* Add a field to the form
*
* @param WebLab_Form_Field $field
* @throws WebLab_Exception_Form
*/
public function add( WebLab_Form_Field $field ){
if( empty( $field ) )
throw new WebLab_Exception_Form( 'Field not set' );
$field_name = $field->name;
if( empty( $field_name ) )
throw new WebLab_Exception_Form( 'Cannot add a field to a form without the field having a name.' );
$field_exists = isset( $this->_fields[ $field_name ] );
if( $field_exists )
throw new WebLab_Exception_Form( 'Duplicate name attribute for fields.' );
$this->_fields[ $field_name ] = $field;
$field->setForm( $this );
return $this;
}
/**
* Remove a field from the form
*
* @param string|WebLab_Form_Field $field
* @throws WebLab_Exception_Form
* @return WebLab_Form
*/
public function remove( $field ){
if( !$field instanceof WebLab_Form_Field && !is_string( $field ) )
throw new WebLab_Exception_Form( 'The field to remove should be either of type WebLab_Form_Field or string.' );
if( is_string( $field ) ) {
unset( $this->_fields[ $field ] );
return $this;
}
if( $field instanceof WebLab_Form_Field && empty( $field->name ) )
throw new WebLab_Exception_Form( 'Cannot remove a field from a form without the field having a name.' );
$set = &$this->_fields[ $field->name ];
if( empty( $set ) )
return $this;
foreach( $set as $key => $value ) {
if( $value == $field )
unset( $set[ $key ] );
}
return $this;
}
/**
* Generates a ID by which the form can identify itself.
*
* @return string
*/
public function getFormId(){
$fields = array_keys( $this->_fields );
$fields = implode( ',', $fields );
return md5( $this->_action . '-' . $this->_method . '-' . $fields );
}
/**
* Determine if we are dealing with a postback.
*
* @return boolean
*/
public function isPostback(){
$postback_code = $this->getFormId();
$response = $this->getResponse();
return isset( $response[$postback_code] );
}
/**
* Retrieve the correct response set
*
* @return array
*/
public function getResponse(){
return ( $this->_method == self::POST ) ? $_POST : $_GET;
}
/**
* Determine if all fields are filled out correctly.
*
* @return boolean
*/
public function isValid(){
foreach( $this->_fields as $key => $field ){
$response = $field->isValid();
if( $response !== true )
$this->_errors[$key] = $response;
}
return ( count( $this->_errors ) == 0 );
}
/**
* Return method being used.
*
* @return string
*/
public function getMethod(){
return $this->_method;
}
/**
* Set method form is going to use.
*
* @param string $method
* @throws WebLab_Exception_Form
* @return WebLab_Form
*/
public function setMethod( $method ){
if( !is_string( $method ) )
throw new WebLab_Exception_Form( 'Method should be either POST or GET');
if( $method == 'POST' || $method == 'GET' )
$this->_method = $method;
return $this;
}
/**
* Return the action that this form is configured with.
*
* @return string
*/
public function getAction(){
return $this->_action;
}
/**
* Set the action to be used by this form
*
* @param string $action
* @throws Exception
* @return WebLab_Form
*/
public function setAction( $action ){
if( !is_string( $action ) )
throw new Exception( 'Action should be a string, pointing to the result page' );
$this->_action = $action;
return $this;
}
/**
* Get a list of the fields.
*
* @return array
*/
public function getFields(){
return $this->_fields;
}
/**
* Update fields to correspond with posted value.
*
*/
public function update(){
foreach( $this->_fields as $field ){
$field->update();
}
}
/**
* Retrieve validation errors.
*
* @return multitype:
*/
public function getErrors(){
return $this->_errors;
}
/**
* Returns a field with $name.
*
* @param string $name
* @return WebLab_Form_Field
* @deprecated
*/
public function __get( $name ){
return $this->_fields[ $name ];
}
/**
* Returns a field with $name
*
* @param string $name
* @return WebLab_Form_Field|array
*/
public function get( $name ) {
return isset( $this->_fields[ $name ] ) ? $this->_fields[ $name ] : null;
}
/**
* Returns whether a field is set.
*
* @param string $name
* @return boolean
* @deprecated
*/
public function __isset( $name ) {
return isset( $this->_fields[$name] );
}
/**
* Get the value of a field
*
* @param unknown_type $field
* @throws WebLab_Exception_Form
* @return string
*/
public function getValue( $field ) {
if( $field instanceof WebLab_Form_Field ) {
$field = $field->getAttribute( 'name' );
}
// name[]
$name = strpos( $field, '[' );
if( $name !== false ) {
$name = substr( $field, 0, $name );
} else {
$name = $field;
$field = null;
}
if( !isset( $this->_fields[ $name ] ) )
throw new WebLab_Exception_Form( 'The field to remove should be either of type WebLab_Form_Field or string.' );
$resp = $this->getResponse();
if( empty( $field ) ) {
$value = isset( $resp[ $name ] ) ? $resp[ $name ] : null;
} else {
$value = isset( $resp[ $name ] ) ? $resp[ $name ] : null;
preg_match_all( '#\[([^\]]+)\]#', $field, $matches, PREG_SET_ORDER );
foreach( $matches as $match ) {
if( empty( $value ) ) break;
@$value = $value[ $match[1] ];
}
}
return $value;
}
/**
* Get all fields their values.
*
* @return array
*/
public function getValues() {
$values = array();
foreach( $this->_fields as $field ) {
$values[ $field->name ] = $field->getValue();
}
return $values;
}
/**
* Set value for a fields.
*
* @param array $obj
* @return WebLab_Form
*/
public function setValues( $obj ) {
foreach( $obj as $field => $value ) {
if( isset( $this->_fields[ $field ] ) ) {
$this->_fields[ $field ]->setValue( $value );
}
}
return $this;
}
/**
* Get all fields their values.
*
* @return array
* @deprecated
*/
public function getResults(){
return $this->getValues();
}
}