My favorites | Sign in
Project Logo
                
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
<?php
/**
*
* Form element class.
*
* @category Solar
*
* @package Solar_Form
*
* @author Paul M. Jones <pmjones@solarphp.com>
*
* @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
* @version $Id$
*
*/
class Solar_Form_Element extends Solar_Struct
{
/**
*
* Default values for each element.
*
* Keys are ...
*
* `name`
* : (string) The name attribute.
*
* `type`
* : (string) The input or type attribute ('text', 'select', etc).
*
* `label`
* : (string) A short label for the element.
*
* `descr`
* : (string) A longer description of the element, such as a tooltip
* or help text.
*
* `value`
* : (string) The default or selected value(s) for the element.
*
* `status`
* : (bool) Whether or not the particular element has passed or failed
* validation (true or false), or null if there has been no attempt at
* validation.
*
* `require`
* : (bool) Whether or not the element is required.
*
* `disable`
* : (bool) If disabled, the element is read-only (but is still
* submitted with other elements).
*
* `options`
* : (array) The list of allowed values as options for this element
* as an associative array in the form (value => label).
*
* `attribs`
* : (array) Additional XHTML attributes for the element in the
* form (attribute => value).
*
* `invalid`
* : (array) An array of messages if the value is invalid.
*
* @var array
*
*/
protected $_default_data = array(
'name' => null,
'type' => null,
'label' => null,
'descr' => null,
'value' => null,
'status' => null,
'require' => false,
'disable' => false,
'options' => array(),
'attribs' => array(),
'filters' => array(),
'invalid' => array(),
);

/**
*
* Constructor.
*
* @param array $config User-defined values.
*
*/
public function __construct($config = null)
{
parent::__construct($config);

// set default values
if (empty($this->_data)) {
$this->_data = $this->_default_data;
} else {
$this->_data = array_merge($this->_default_data, $this->_data);
$this->_fixData();
}
}

/**
*
* Loads the struct with data from an array or another struct.
*
* @param array|Solar_Struct $spec The data to load into the object.
*
*/
public function load($spec)
{
parent::load($spec);
$this->_fixData();
}

/**
*
* Fixes all data, forcing __set() (and thus the set methods) to be used.
*
*/
protected function _fixData()
{
foreach ($this->_data as $key => $value) {
// don't override existing properties
if (! property_exists($this, $key)) {
$this->$key = $value;
}
}
}

// -----------------------------------------------------------------
//
// Set methods
//
// -----------------------------------------------------------------

/**
*
* Sets a key value.
*
* @param string $key The requested data key.
*
* @param mixed $val The value to set the data to.
*
*/
public function __set($key, $val)
{
// look for a set method to fix the value if needed
$method = 'set' . ucfirst($key);

if (method_exists($this, $method)) {
// use set method
$this->$method($val);
} else {
// set method is not defined: just set the value
$this->_data[$key] = $val;
}
}

/**
*
* Sets the name of the element.
*
* @param string $name The element name.
*
*/
public function setName($name)
{
$this->_data['name'] = (string) $name;
}

/**
*
* Sets the type of the element.
*
* @param string $type The element type ('text', 'select', etc).
*
*/
public function setType($type)
{
$this->_data['type'] = (string) $type;
}

/**
*
* Sets the element label.
*
* @param string $label The element label.
*
*/
public function setLabel($label)
{
$this->_data['label'] = (string) $label;
}

/**
*
* Sets the element description.
*
* @param string $descr The element description.
*
*/
public function setDescr($descr)
{
$this->_data['descr'] = (string) $descr;
}

/**
*
* Sets if the element has passed or failed validation.
*
* @param bool $status True if the element is valid, false otherwise.
*
*/
public function setStatus($status)
{
if ($status === null) {
// null is a valid value, meaning that the form has not passed
// by a validation process yet
$this->_data['status'] = null;
} else {
$this->_data['status'] = (bool) $status;
}
}

/**
*
* Sets if the element is required.
*
* @param bool $require True if the element is required, false otherwise.
*
*/
public function setRequire($require)
{
$this->_data['require'] = (bool) $require;
}

/**
*
* Sets if the element is disabled.
*
* @param bool $disable True if the element is disabled, false otherwise.
*
*/
public function setDisable($disable)
{
$this->_data['disable'] = (bool) $disable;
}

/**
*
* Sets element options.
*
* @param array $options Element options.
*
*/
public function setOptions($options)
{
if (! is_array($options)) {
// only cast if it is not already an array, otherwise its messes
// with the array cursor or something like that
$options = (array) $options;
}

$this->_data['options'] = $options;
}

/**
*
* Sets the attributes of the element.
*
* @param array $attribs Set these attribs on the element; this will reset
* (and not override/increment) the attributes
*
*/
public function setAttribs($attribs)
{
if (! is_array($attribs)) {
// only cast if it is not already an array, otherwise its messes
// with the array cursor or something like that
$attribs = (array) $attribs;
}

$this->_data['attribs'] = $attribs;
}

/**
*
* Sets the element filters.
*
* @param array $filters Element filters.
*
*/
public function setFilters($filters)
{
if (! is_array($filters)) {
// only cast if it is not already an array, otherwise its messes
// with the array cursor or something like that
$filters = (array) $filters;
}

$this->_data['filters'] = $filters;
}

/**
*
* Sets invalid messages for the element.
*
* @param array $spec Invalid messages.
*
*/
public function setInvalid($spec)
{
// reset and re-add the messages.
$this->_data['invalid'] = array();
$this->addInvalid($spec);
}

// -----------------------------------------------------------------
//
// Add methods
//
// -----------------------------------------------------------------

/**
*
* Adds one or more attributes of the element.
*
* @param string|array $spec An atribute key or an array of key => value.
*
* @param mixed $value Attribute value.
*
*/
public function addAttribs($spec, $value = null)
{
if (! is_array($spec)) {
// $spec is just an attribute key
$spec = array($spec => $value);
}

foreach($spec as $key => $value) {
if (is_string($key) && !empty($value)) {
$this->_data['attribs'][$key] = $value;
}
}
}

/**
*
* Adds one or more invalid message to the element and sets the element
* status to false (invalid).
*
* @param string|array $spec The invalidation message(s).
*
*/
public function addInvalid($spec)
{
// add the messages
foreach ((array) $spec as $text) {
$this->_data['invalid'][] = (string) $text;
}

// mark the status of the element
if (! empty($this->_data['invalid'])) {
$this->_data['status'] = false;
}
}

/**
*
* Adds one filter to the element.
*
* @param array|string $spec The filter specification; either a
* Solar_Filter method name (string), or an array where the first element
* is a method name and remaining elements are parameters to that method.
*
*/
public function addFilter($spec)
{
$this->_data['filters'][] = (array) $spec;
}
}
Show details Hide details

Change log

r1569 by rodrigo.moraes on Sep 22, 2008   Diff
Added addAttribs().
Go to: 
Project members, sign in to write a code review

Older revisions

r1568 by rodrigo.moraes on Sep 21, 2008   Diff
- Weird bug: casting an array to array
seems to mess with the internal cursor
or something like that; now methods
that requires casting properties to
arrays only do that if the passed
...
r1567 by rodrigo.moraes on Sep 21, 2008   Diff
- Added back _fixData(), because it is
not only used by load(). It can also
be used on construction if data is
passed as config key.
r1566 by rodrigo.moraes on Sep 21, 2008   Diff
- Added __set(), forcing set methods
to be used when available.
- Added set methods for element
properties that require casting.
- Renamed $_data to $_default_data,
...
All revisions of this file

File info

Size: 9446 bytes, 383 lines

File properties

svn:keywords
Id
Hosted by Google Code