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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
<?php
/**
*
* Form-processing class; also hints the view on how to present the form.
*
* @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 extends Solar_Base
{
/**
*
* User-provided configuration.
*
* Keys are ...
*
* `request`
* : (dependency) A Solar_Request dependency object.
*
* `attribs`
* : (array) An array of <form> tag attributes; used for hinting
* the view on how to present the form. Defaults are 'method="post"',
* 'action="REQUEST_URI"', and 'enctype="multipart/form-data"'.
*
* `success`
* : (string) The overall "success" message when validating form
* input. Default is Solar locale key SUCCESS_FORM.
*
* `failure`
* : (string) The overall "failure" message when validating form
* input. Default is Solar locale key FAILURE_FORM.
*
* `filter`
* : (dependency) A Solar_Filter dependency injection; default is empty,
* which creates a standard Solar_Filter object on the fly.
*
* @var array
*
*/
protected $_Solar_Form = array(
'filter' => null,
'success' => null,
'failure' => null,
'attribs' => array(),
);

/**
*
* The validation status of the form.
*
* @var bool Null if validation has not occurred yet, true if
* valid, false if not valid.
*
*/
protected $_status = null;

/**
*
* Default <form> tag attributes.
*
* @var array
*
*/
protected $_default_attribs = array(
'action' => null,
'method' => 'post',
'enctype' => 'multipart/form-data',
);

/**
*
* Attributes for the form tag itself.
*
* The `$attribs` array holds HTML attributes for the
* form itself (not for individual elements) such as
* `action`, `method`, and `enctype`. Note that these
* are "hints" for the presentation of the form, and may not
* be honored by the view.
*
* @var array
*
*/
public $attribs = array();

/**
*
* The array of elements in this form.
*
* The `$elements` array contains all elements in the form,
* including their names, types, values, any invalidation messages,
* filter callbacks, and so on.
*
* In general, you should not try to set $elements yourself;
* instead, Solar_Form::setElement() and Solar_Form::setElements().
*
* @var array
*
*/
public $elements = array();

/**
*
* Overall feedback about the state of the form.
*
* The `$feedback` array stores feedback messages for
* the form itself (not for individual elements). For example,
* "Saved successfully." or "Please correct the noted errors."
* Each array element is an additional feedback message.
*
* Note that the $feedback property pertains to the form as a
* whole, not the individual elements. This is as opposed to
* the 'invalid' key in each of the elements, which contains
* invalidation messages specific to that element.
*
* @var array
*
*/
public $feedback = array();

/**
*
* The array of filters for the form elements.
*
* @var array
*
*/
protected $_filters = array();

/**
*
* Array of submitted values.
*
* Populated on the first call to [[_populate()]], which itself uses
* [[Solar_Request::get()]] or [[Solar_Request::post()]], depending on
* the value of $this->attribs['method'].
*
* @var array
*
* @todo Do we really need this as a property?
*
*/
protected $_submitted = null;

/**
*
* A Solar_Filter object for filtering form values.
*
* @var Solar_Filter
*
* @see setFilterLocaleObj()
*
*/
protected $_filter;

/**
*
* Request environment object.
*
* @var Solar_Request
*
*/
protected $_request;

/**
*
* Constructor.
*
* @param array $config User-provided configuration values.
*
*/
public function __construct($config = null)
{
// programmatic defaults
$this->_Solar_Form['success'] = $this->locale('SUCCESS_FORM');
$this->_Solar_Form['failure'] = $this->locale('FAILURE_FORM');

// "real" contruction
parent::__construct($config);

// request environment
$this->_request = Solar_Registry::get('request');

// filter object
$this->_filter = Solar::dependency(
'Solar_Filter',
$this->_config['filter']
);

// set the default action attribute
$action = $this->_request->server('REQUEST_URI');
$this->_default_attribs['action'] = $action;

// now merge attribute configs
$this->_config['attribs'] = array_merge(
$this->_default_attribs,
$this->_config['attribs']
);

// reset all the properties based on config now
$this->reset();
}

// -----------------------------------------------------------------
//
// Element-management methods
//
// -----------------------------------------------------------------

/**
*
* Gets one element from the form.
*
* @param string $name The element name to get.
*
* @param string $array Rename the element as a key in this array.
*
* @param bool $throw If true, throws an exception if the element is not
* found.
*
* @return Solar_Form_Element
*
*/
public function getElement($name, $array = null, $throw = false)
{
$name = $this->_prepareName($name, $array);

if (empty($this->elements[$name])) {
if ($throw) {
throw $this->_exception('ERR_NO_SUCH_ELEMENT', array(
'name' => $name,
));
} else {
return false;
}
}

return $this->elements[$name];
}

/**
*
* Returns all elements of a certain type.
*
* @param string $type The element type.
*
* @return array List of elements.
*
*/
public function getElementsByType($type)
{
$list = array();

foreach ($this->elements as $name => $elem) {
if ($elem['type'] == $type) {
$list[$name] = $elem;
}
}

return $list;
}

/**
*
* Sets one element in the form. Appends if element does not exist.
*
* @param string $name The element name to set or add; overrides
* $info['name'].
*
* @param array|Solar_Form_Element $spec Element object or array using the
* same keys as in Solar_Form_Element::$_data.
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function setElement($name, $spec, $array = null)
{
// prepare the name as an array key?
$name = $this->_prepareName($name, $array);

if ($spec instanceof Solar_Form_Element) {
$elem = $spec;
} else {
// set the name as part of the element data
$spec['name'] = $name;

// create the new element
$elem = Solar::factory('Solar_Form_Element');
$elem->load($spec);
}

// retain info as an element.
$this->elements[$name] = $elem;
}

/**
*
* Sets multiple elements in the form. Appends if they do not exist.
*
* @param array $spec Element information as array(name => spec), where
* each spec value is a Solar_Form_Element instance or an array like
* Solar_Form_Element::$_data.
*
* @param string $array Rename each element as a key in this array.
*
* @return void
*
*/
public function setElements($list, $array = null)
{
foreach ($list as $name => $spec) {
$this->setElement($name, $spec, $array);
}
}

/**
*
* Sets the attributes of one element.
*
* @param string $name The element name.
*
* @param array $attribs Set these attribs on the element; the key is the
* attribute name, and the value is the attribute value.
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function setAttribs($name, $attribs, $array = null)
{
if ($elem = $this->getElement($name, $array)) {
$elem->setAttribs($attribs);
}
}

/**
*
* Sets the type of one element.
*
* @param string $name The element name.
*
* @param string $type The element type ('text', 'select', etc).
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function setType($name, $type, $array = null)
{
if ($elem = $this->getElement($name, $array)) {
$elem->setType($type);
}
}

/**
*
* Reorders the existing elements.
*
* @param array $list The order in which elements should be placed; each
* value in the array is an element name.
*
* @return void
*
*/
public function orderElements($list)
{
// the set of elements as they are now
$old = $this->elements;
// reset the elements to blank
$this->elements = array();
// put the elements in the requested order
foreach ((array) $list as $name) {
if (isset($old[$name])) {
$this->elements[$name] = $old[$name];
}
}
// retain all remaining old elements
foreach ($old as $name => $info) {
$this->elements[$name] = $info;
}
// done!
}

/**
*
* Tells the internal filter what object it should use for locale
* translations.
*
* @param Solar_Base $obj The object to use for locale translations.
*
* @return void
*
*/
public function setFilterLocaleObject($obj)
{
$this->_filter->setChainLocaleObject($obj);
}

/**
*
* Adds one filter to an element.
*
* @param string $name The element name.
*
* @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.
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function addFilter($name, $spec, $array = null)
{
if ($elem = $this->getElement($name, $array)) {
$elem->addFilter($spec);
}
}

/**
*
* Adds many filters to one element.
*
* @param string $name The element name.
*
* @param array|string $list The list of filters for this element.
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function addFilters($name, $list, $array = null)
{
foreach ((array) $list as $spec) {
$this->addFilter($name, $spec, $array);
}
}

/**
*
* Adds one or more invalid message to an element, sets the element status
* to false (invalid), and sets the form status to false (invalid).
*
* @param string $name The element name.
*
* @param string|array $spec The invalidation message(s).
*
* @param string $array Rename each element as a key in this array.
*
* @return void
*
*/
public function addInvalid($name, $spec, $array = null)
{
if ($elem = $this->getElement($name, $array)) {
$elem->addInvalid($spec);
$this->_status = false;
}
}

/**
*
* Adds invalidation messages to multiple elements, sets their status to
* false (invalid) and sets the form status to false (invalid).
*
* @param array $list An array where the key is the element name, and the
* value is a string or array of invalidation messages for that element.
*
* @param string $array Rename each element as a key in this array.
*
* @return void
*
*/
public function addInvalids($list, $array = null)
{
foreach ((array) $list as $name => $spec) {
$this->addInvalid($name, $spec, $array);
}
}

// -----------------------------------------------------------------
//
// Value-management methods
//
// -----------------------------------------------------------------

/**
*
* Manually set the value of one element.
*
* Note that this is subtly different from [[populate()]]. This method
* takes full name of the element, whereas populate() takes a "natural"
* hierarchical array like $_POST.
*
* @param string $name The element name.
*
* @param mixed $value Set the element to this value.
*
* @param string $array Rename the element as a key in this array.
*
* @return void
*
*/
public function setValue($name, $value, $array = null)
{
if ($elem = $this->getElement($name, $array)) {
$elem->setValue($value);
}
}

/**
*
* Manually set the value of several elements.
*
* Note that this is subtly different from [[populate()]]. This method
* takes a flat array where the full name of the element is the key,
* whereas populate() takes a "natural" hierarchical array like $_POST.
*
* @param array $data An associative array where the key is the element
* name and the value is the element value to set.
*
* @param string $array Rename each element as a key in this array.
*
* @return void
*
* @throws Solar_Form_Exception_NoSuchElement when the named element does
* not exist in the form.
*
*/
public function setValues($data, $array = null)
{
foreach ((array) $data as $name => $value) {
$this->setValue($name, $value, $array);
}
}

/**
*
* Automatically populates form elements with specified values.
*
* @param array $submit The source data array for populating form
* values as array(name => value); if null, will populate from POST
* or GET vars as determined from the Solar_Form::$attribs['method']
* value.
*
* @return void
*
*/
public function populate($submit = null)
{
$this->_submitted = array();
$this->_status = null;

// import the submitted values
if (is_array($submit)) {
// from an array
$this->_submitted = $submit;
} elseif (is_object($submit)) {
// from an object
$this->_submitted = (array) $submit;
} else {
// from GET or POST, per the form method.
$method = strtolower($this->attribs['method']);
if ($method == 'get' || $method == 'post') {
$this->_submitted = $this->_request->$method();
}
}

// populate the submitted values into the
// elements themsevles.
$this->_populate($this->_submitted);
}

/**
*
* Applies the filter chain to the form element values; in particular,
* checks validation and updates the 'invalid' keys for each element that
* fails.
*
* This method cycles through each element in the form, where it ...
*
* 1. Applies the filters to populated user input for the element,
*
* 2. Validates the filtered value against the validation rules for the element,
*
* 3. Adds invalidation messages to the element if it does not pass validation.
*
* If all populated values pass validation, the method returns boolean
* true, indicating the form as a whole it valid; if even one validation on
* one element fails, the method returns boolean false.
*
* In general, you should only validate the values after user input has
* been populated with [[Solar_Form::populate()]].
*
* Note that filters and validation rules are added with the
* [[Solar_Form::setElement()]] and [[Solar_Form::setElements()]] methods;
* please see those pages for more information on how to add filters and
* validation to an element.
*
* @return bool True if all elements are valid, false if not.
*
*/
public function validate()
{
// reset the filter chain so we can rebuild it
$this->_filter->resetChain();

// build the filter chain and data values. note that the foreach()
// loop uses an info **reference**, not a copy.
$data = array();
foreach ($this->elements as $name => $elem) {
// keep a **copy** of the data (not a reference)
$data[$name] = $elem->value;

// set the filters and require-flag, reference not needed
$this->_filter->addChainFilters($name, $elem->filters);
$this->_filter->setChainRequire($name, $elem->require);

}

// apply the filter chain to the data, which will modify the
// element data in place because of the reference
$this->_status = $this->_filter->applyChain($data);

// put the filtered values back to the elements
foreach ($data as $name => $value) {
$this->elements[$name]->setValue($value);
}

// set the main feedback message
if ($this->_status) {
$this->feedback = array($this->_config['success']);
} else {
$this->feedback = array($this->_config['failure']);
}

// retain any invalidation messages
$invalid = $this->_filter->getChainInvalid();
foreach ((array) $invalid as $key => $val) {
$this->addInvalid($key, $val);
}

// done!
return $this->_status;
}

/**
*
* Returns the form element values as an array.
*
* @param string $key Return only values that are part of
* this array key. If null, returns all values in the
* form.
*
* @return array An associative array of element values.
*
*/
public function getValues($key = null)
{
$values = array();
foreach ($this->elements as $name => $elem) {
$this->_values($name, $elem['value'], $values);
}

if (! $key) {
return $values;
}

if (! empty($values[$key])) {
return $values[$key];
}
}

/**
*
* Returns one form element value.
*
* @param string $key The element key, including the array name (if any).
*
* @return mixed The element value.
*
*/
public function getValue($key)
{
if (array_key_exists($key, $this->elements)) {
return $this->elements[$key]['value'];
}
}

// -----------------------------------------------------------------
//
// General-purpose methods
//
// -----------------------------------------------------------------

/**
*
* Resets the form object to its originally-configured state.
*
* This clears out all elements, filters, validations, and feedback,
* as well as all submitted values. Use this method to "start over
* again" using the same form object.
*
* @return void
*
*/
public function reset()
{
$this->attribs = $this->_config['attribs'];
$this->elements = array();
$this->feedback = null;
$this->_filters = array();
$this->_submitted = null;
}

/**
*
* Forcibly sets the overall form validation status.
*
* Does not set individual element status values.
*
* @param bool $flag True if you want to say the form is valid,
* false if you want to say it is not valid.
*
* @return void
*
*/
public function setStatus($flag)
{
$this->feedback = array();
if ($flag === null) {
$this->_status = null;
} elseif ((bool) $flag) {
$this->_status = true;
$this->feedback = array($this->_config['success']);
} else {
$this->_status = false;
$this->feedback = array($this->_config['failure']);
}
}

/**
*
* Gets the current overall form validation status.
*
* @return bool True if valid, false if not valid, null if validation
* has not been attempted.
*
*/
public function getStatus()
{
return $this->_status;
}

/**
*
* Loads form attributes and elements from an external source.
*
* You can pass an arbitrary number of parameters to this method;
* all params after the first will be passed as arguments to the
* fetch() method of the loader class.
*
* The loader class itself must have at least one method, fetch(),
* that returns an associative array with keys 'attribs' and
* 'elements' which contain, respectively, values for $this->attribs
* and $this->setElements().
*
* {{code: php
* $form = Solar::factory('Solar_Form');
* $form->load('Solar_Form_Load_Xml', '/path/to/form.xml');
* }}
*
* @param string|object $obj If a string, it is treated as a class
* name to instantiate with [[Solar::factory()]]; if an object, it is
* used as-is. Either way, the fetch() method of the object will
* be called to populate this form (via $this->attribs property and
* the $this->setElements() method).
*
* @return void
*
*/
public function load($obj)
{
// if the first param is a string class name
// try to instantiate it.
if (is_string($obj)) {
$obj = Solar::factory($obj);
}

// if we *still* don't have an object, or if there's no
// fetch() method, there's a problem.
if (! is_object($obj) ||
! is_callable(array($obj, 'fetch'))) {
throw $this->_exception(
'ERR_METHOD_NOT_CALLABLE',
array('method' => 'fetch')
);
}

// get any additional arguments to pass to the fetch
// method (after dropping the first param) ...
$args = func_get_args();
array_shift($args);

// ... and call the fetch method.
$info = call_user_func_array(
array($obj, 'fetch'),
$args
);

// we don't call reset() because there are
// sure to be cases when you need to load()
// more than once to get a full form.
//
// merge the loaded attribs onto the current ones.
$this->attribs = array_merge(
$this->attribs,
$info['attribs']
);

// add elements, overwriting existing ones (no way
// around this, I'm afraid).
$this->setElements($info['elements']);
}

// -----------------------------------------------------------------
//
// Support methods
//
// -----------------------------------------------------------------

/**
*
* Prepares a name as an array key, if needed.
*
* @param string $name The element name.
*
* @param string $array The array name, if any, into which we place
* the element.
*
* @return string The prepared element name.
*
*/
protected function _prepareName($name, $array = null)
{
if ($array) {
$pos = strpos($name, '[');
if ($pos === false) {
// name is not itself an array.
// for example, 'field' becomes 'array[field]'
$name = $array . "[$name]";
} else {
// the name already has array keys, for example
// 'field[0]'. make the name just another key
// in the array, for example 'array[field][0]'.
$name = $array . '[' .
substr($name, 0, $pos) . ']' .
substr($name, $pos);
}
}
return $name;
}

/**
*
* Recursive method to map the submitted values into elements.
*
* @param array|string $src The source data populating form
* values. If an array, will recursively descend into the array;
* if a scalar, will map the value into a form element.
*
* @param string $name The name of the current element mapped from
* the array of submitted values. For example, $src['foo']['bar']['baz']
* maps to "foo[bar][baz]".
*
* @return void
*
*/
protected function _populate($src, $name = null)
{
// are we working with an array?
if (is_array($src)) {

// sequential arrays are generally multiple-select items.
// only check the first key on the array.
$is_sequential = is_int(key($src));

// temporal values may also be expressed as arrays
$types = array('date', 'time', 'timestamp');
$is_temporal = isset($this->elements[$name]) &&
in_array($this->elements[$name]['type'], $types);

// retain value as-is, or descend through sub-elements?
if ($is_sequential || $is_temporal) {
// retain value as-is (no recursive descent)
$this->elements[$name]['value'] = $src;
} else {
// not sequential, not temporal. descend through each of the
// sub-elements.
foreach ($src as $key => $val) {
$sub = empty($name) ? $key : $name . "[$key]";
$this->_populate($val, $sub);
}
}

} elseif (isset($this->elements[$name])) {

// convenient reference
$elem =& $this->elements[$name];

// do not populate certain elements, as this will
// reset their value inappropriately.
$skip = $name['type'] == 'submit' ||
$name['type'] == 'button' ||
$name['type'] == 'reset';

if ($skip) {
return;
}

// is this a multiple select?
$multiple = $elem['type'] == 'select' &&
! empty($elem['attribs']['multiple']);

// set the value appropriately
if ($multiple && ! $src) {
// empty on a multiple. force it to an empty array.
// (merely casting to array gets us an array with one
// empty-string value.)
$elem['value'] = array();
} else {
$elem['value'] = $src;
}
}
}

/**
*
* Recursively pulls values from elements into an associative array.
*
* @param string $key The current array key for the values array. If
* this has square brackets in it, that's a sign we need to keep creating
* sub-elements for the values array.
*
* @param mixed $val The element value to put into the values array, once
* we stop creating sub-elements based on the element name.
*
* @param array &$values The values array into which we will place the
* element value. Note that it becomes a reference to sub-elements as
* the recursive function creates new sub-elements based on the form
* element name.
*
* @return void
*
*/
protected function _values($key, $val, &$values)
{
if (strpos($key, '[') === false) {

// no '[' in the key, so we're at the end
// of any recursive descent; capture the value.
if (empty($key)) {
// handles elements named as auto-append arrays '[]'
$values[] = $val;
} else {
$values[$key] = $val;
}
return;

} else {

// recursively parse the element name ($key) to create an
// array-key for its value.
//
// $key is something like "foo[bar][baz]".
//
// 0123456789012
// foo[bar][baz]
//
// find the first '['.
$pos = strpos($key, '[');

// the part before the '[' is the new value key
$new = substr($key, 0, $pos);

// the part after the '[' still needs to be processed
$key = substr($key, $pos+1);

// create $values['foo'] if it does not exist.
if (! isset($values[$new])) {
$values[$new] = null;
}

// now $key is something like "bar][baz]".
//
// 012345678
// bar][baz]
//
// remove the first remaining ']'. this should leave us
// with 'bar[baz]'.
$pos = strpos($key, ']');
$key = substr_replace($key, '', $pos, 1);


// continue to descend,
// but relative to the new value array.
$this->_values($key, $val, $values[$new]);
}
}
}
Show details Hide details

Change log

r1605 by rodrigo.moraes on Oct 05, 2008   Diff
Fixed validate() to not use references to
element data: was causing a "Indirect
modification of overloaded element of
Solar_Form_Element..."
Go to: 
Project members, sign in to write a code review

Older revisions

r1603 by rodrigo.moraes on Oct 03, 2008   Diff
More fixes: model records set values
to *all* its columns in the form,
which results in exceptions because
the elements are not found. Now
getElement() returns false if the
...
r1563 by rodrigo.moraes on Sep 20, 2008   Diff
Solar_Form branch, with form elements
as proper objects instead of simple
arrays. Initial commit.
All revisions of this file

File info

Size: 30637 bytes, 1025 lines

File properties

svn:executable
*
svn:keywords
Id
Hosted by Google Code