-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-field.php
54 lines (40 loc) · 1.22 KB
/
form-field.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
<?php
/**
*
* Description: Form field automation class
* Author: Denis Vororpaev
* Author Email: [email protected]
* Version: 0.1.0
* Copyright: Denis Voropaev © 2016
*
**/
class Field {
public $name;
public $type;
public $value;
public $title;
public $isRequired;
public function __construct ($name, $type, $title, $isRequired, $value = null) {
$this->name = strval ($name);
$this->type = strval ($type);
$this->title = strval ($title);
$this->isRequired = $isRequired ? true : false;
if ($value) { $this->setValue ($value); }
}
public function setValue ($value) {
$this->value = secure_input ($value);
}
public function render () {
$value = !empty ($this->value) ? $this->value : '';
$optional = !$this->isRequired ? ' (Optional)' : '';
$required = $this->isRequired ? ' required' : '';
$input = ($this->type === 'textarea') ?
sprintf ('<textarea id="%s" name="%s"%s>%s</textarea>',
$this->name, $this->name, $required, $value) :
sprintf ('<input id="%s" name="%s" type="%s" value="%s"%s/>',
$this->name, $this->name, $this->type, $value, $required);
return sprintf ('<label><span>%s%s</span>%s</label>',
$this->title, $optional, $input);
}
}
?>