-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFormField.php
More file actions
200 lines (162 loc) · 4.25 KB
/
AbstractFormField.php
File metadata and controls
200 lines (162 loc) · 4.25 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
<?php
/**
* This file is part of the CarteBlanche PHP framework.
*
* (c) Pierre Cassat <me@e-piwi.fr> and contributors
*
* License Apache-2.0 <http://github.com/php-carteblanche/carteblanche/blob/master/LICENSE>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tool\Form;
use \CarteBlanche\CarteBlanche;
use \CarteBlanche\App\Kernel;
use \Library\Helper\Html;
abstract class AbstractFormField
{
var $field;
protected $label=null;
protected $name=null;
protected $value=null;
protected $errors=array();
protected $options=array();
protected $attributes=array();
static $html5_allowed = true;
public function __construct( $name, $value, $label, $options=null, $attributes=null )
{
$this->name = $name;
$this->value = $value;
$this->label = $label;
if (!is_null($options))
$this->options = array_merge($this->options, $options);
if (!is_null($attributes))
$this->attributes = array_merge($this->attributes, $attributes);
}
public function __toString()
{
$this->build();
return $this->field;
}
// ---------------
// GETTERS
// ---------------
public function getOption( $name )
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}
public function getOptions()
{
return $this->options;
}
public function getAttribute( $name )
{
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
}
public function getAttributes()
{
return $this->attributes;
}
public function getName()
{
return $this->name;
}
public function getErrors()
{
return $this->errors;
}
public function getErrorsAsString()
{
return is_array($this->errors) ? join(' ', $this->errors) : $this->errors;
}
public function hasErrors()
{
return !empty($this->errors);
}
public function getValue()
{
return $this->value;
}
public function getLabel()
{
return $this->label;
}
public function prepend()
{
return isset($this->options['html_before']) ? $this->options['html_before'] : '';
}
public function append()
{
return isset($this->options['html_after']) ? $this->options['html_after'] : '';
}
// ---------------
// Rendering & treatments
// ---------------
/**
* Build the field HTML string
*/
public function build()
{
$this->field = $this->prepend()
.'<div class="field">'
.( $this->getLabel() ? '<label for="'.$this->getName().'">'.$this->getLabel().'</label>' : '' )
.( $this->hasErrors() ? '<span class="error">'.$this->getErrorsAsString().'</span>' : '' )
.$this->buildField()
.$this->getOption('comment')
.'</div>'
.$this->append();
return $this->field;
}
/**
* Treat form values setting errors if so
*/
public function treat()
{
$this->value = CarteBlanche::getContainer()->get('request')->getPost($this->getName(), '');
$validation_rules = $this->getOption('validation_rules');
// required field
if ($this->getAttribute('required') && $this->getAttribute('required')=='true' && $this->getValue()=='')
{
$this->errors[] = 'This field is required';
}
// numeric fields
elseif ($this->getOption('numeric') && $this->getOption('numeric')==true &&
($this->getValue()!='' && !is_numeric($this->getValue()))
){
$this->errors[] = 'This field only accepts numeric values';
}
// validation
elseif (!empty($validation_rules))
{
foreach($validation_rules as $fct)
{
//echo '<br />searching validation function "'.$fct.'"';
//echo '<br /> treating validation "'.$fct.'" on value "'.$this->getValue().'"';
$callback = new \CarteBlanche\Library\Callback(
$this->getValue(), array($fct), 'boolean'
);
$response = $callback->getResult();
//echo '<br /> => '.var_export($response,1);
if ($response!==true)
{
$this->errors[] = "This field must pass test function '$fct'";
break;
}
}
}
$val = $this->treatField();
if (!empty($val)) return $val;
return $this->getValue();
}
// ---------------
// Interface
// ---------------
/**
* Build the field HTML string in the sub-class
*/
abstract public function buildField();
/**
* Treat form values setting errors in the sub-class if so
*/
abstract public function treatField();
}
// Endfile