1: <?php
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: class NFormContainer extends NComponentContainer implements ArrayAccess, INamingContainer
27: {
28:
29: public $onValidate;
30:
31:
32: protected $currentGroup;
33:
34:
35: protected $valid;
36:
37:
38:
39:
40:
41:
42:
43: 44: 45: 46: 47: 48:
49: public function setDefaults($values, $erase = FALSE)
50: {
51: $form = $this->getForm(FALSE);
52: if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
53: $this->setValues($values, $erase);
54: }
55: return $this;
56: }
57:
58:
59:
60: 61: 62: 63: 64: 65:
66: public function setValues($values, $erase = FALSE)
67: {
68: if ($values instanceof Traversable) {
69: $values = iterator_to_array($values);
70:
71: } elseif (!is_array($values)) {
72: throw new InvalidArgumentException("Values must be an array, " . gettype($values) ." given.");
73: }
74:
75: $cursor = & $values;
76: $iterator = $this->getComponents(TRUE);
77: foreach ($iterator as $name => $control) {
78: $sub = $iterator->getSubIterator();
79: if (!isset($sub->cursor)) {
80: $sub->cursor = & $cursor;
81: }
82: if ($control instanceof IFormControl) {
83: if ((is_array($sub->cursor) || $sub->cursor instanceof ArrayAccess) && array_key_exists($name, $sub->cursor)) {
84: $control->setValue($sub->cursor[$name]);
85:
86: } elseif ($erase) {
87: $control->setValue(NULL);
88: }
89: }
90: if ($control instanceof INamingContainer) {
91: if ((is_array($sub->cursor) || $sub->cursor instanceof ArrayAccess) && isset($sub->cursor[$name])) {
92: $cursor = & $sub->cursor[$name];
93: } else {
94: unset($cursor);
95: $cursor = NULL;
96: }
97: }
98: }
99: return $this;
100: }
101:
102:
103:
104: 105: 106: 107:
108: public function getValues()
109: {
110: $values = array();
111: $cursor = & $values;
112: $iterator = $this->getComponents(TRUE);
113: foreach ($iterator as $name => $control) {
114: $sub = $iterator->getSubIterator();
115: if (!isset($sub->cursor)) {
116: $sub->cursor = & $cursor;
117: }
118: if ($control instanceof IFormControl && !$control->isDisabled() && !($control instanceof ISubmitterControl)) {
119: $sub->cursor[$name] = $control->getValue();
120: }
121: if ($control instanceof INamingContainer) {
122: $cursor = & $sub->cursor[$name];
123: $cursor = array();
124: }
125: }
126: return $values;
127: }
128:
129:
130:
131:
132:
133:
134:
135: 136: 137: 138:
139: public function isValid()
140: {
141: if ($this->valid === NULL) {
142: $this->validate();
143: }
144: return $this->valid;
145: }
146:
147:
148:
149: 150: 151: 152:
153: public function validate()
154: {
155: $this->valid = TRUE;
156: $this->onValidate($this);
157: foreach ($this->getControls() as $control) {
158: if (!$control->getRules()->validate()) {
159: $this->valid = FALSE;
160: }
161: }
162: }
163:
164:
165:
166:
167:
168:
169:
170: 171: 172: 173:
174: public function setCurrentGroup(NFormGroup $group = NULL)
175: {
176: $this->currentGroup = $group;
177: return $this;
178: }
179:
180:
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function addComponent(IComponent $component, $name, $insertBefore = NULL)
191: {
192: parent::addComponent($component, $name, $insertBefore);
193: if ($this->currentGroup !== NULL && $component instanceof IFormControl) {
194: $this->currentGroup->add($component);
195: }
196: }
197:
198:
199:
200: 201: 202: 203:
204: public function getControls()
205: {
206: return $this->getComponents(TRUE, 'IFormControl');
207: }
208:
209:
210:
211: 212: 213: 214: 215:
216: public function getForm($need = TRUE)
217: {
218: return $this->lookup('NForm', $need);
219: }
220:
221:
222:
223:
224:
225:
226:
227: 228: 229: 230: 231: 232: 233: 234:
235: public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
236: {
237: return $this[$name] = new NTextInput($label, $cols, $maxLength);
238: }
239:
240:
241:
242: 243: 244: 245: 246: 247: 248: 249:
250: public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NULL)
251: {
252: $control = new NTextInput($label, $cols, $maxLength);
253: $control->setPasswordMode(TRUE);
254: return $this[$name] = $control;
255: }
256:
257:
258:
259: 260: 261: 262: 263: 264: 265: 266:
267: public function addTextArea($name, $label = NULL, $cols = 40, $rows = 10)
268: {
269: return $this[$name] = new NTextArea($label, $cols, $rows);
270: }
271:
272:
273:
274: 275: 276: 277: 278: 279:
280: public function addFile($name, $label = NULL)
281: {
282: return $this[$name] = new NFileUpload($label);
283: }
284:
285:
286:
287: 288: 289: 290: 291: 292:
293: public function addHidden($name, $default = NULL)
294: {
295: $control = new NHiddenField;
296: $control->setDefaultValue($default);
297: return $this[$name] = $control;
298: }
299:
300:
301:
302: 303: 304: 305: 306: 307:
308: public function addCheckbox($name, $caption = NULL)
309: {
310: return $this[$name] = new NCheckbox($caption);
311: }
312:
313:
314:
315: 316: 317: 318: 319: 320: 321:
322: public function addRadioList($name, $label = NULL, array $items = NULL)
323: {
324: return $this[$name] = new NRadioList($label, $items);
325: }
326:
327:
328:
329: 330: 331: 332: 333: 334: 335: 336:
337: public function addSelect($name, $label = NULL, array $items = NULL, $size = NULL)
338: {
339: return $this[$name] = new NSelectBox($label, $items, $size);
340: }
341:
342:
343:
344: 345: 346: 347: 348: 349: 350: 351:
352: public function addMultiSelect($name, $label = NULL, array $items = NULL, $size = NULL)
353: {
354: return $this[$name] = new NMultiSelectBox($label, $items, $size);
355: }
356:
357:
358:
359: 360: 361: 362: 363: 364:
365: public function addSubmit($name, $caption = NULL)
366: {
367: return $this[$name] = new NSubmitButton($caption);
368: }
369:
370:
371:
372: 373: 374: 375: 376: 377:
378: public function addButton($name, $caption)
379: {
380: return $this[$name] = new NButton($caption);
381: }
382:
383:
384:
385: 386: 387: 388: 389: 390: 391:
392: public function addImage($name, $src = NULL, $alt = NULL)
393: {
394: return $this[$name] = new NImageButton($src, $alt);
395: }
396:
397:
398:
399: 400: 401: 402: 403:
404: public function addContainer($name)
405: {
406: $control = new NFormContainer;
407: $control->currentGroup = $this->currentGroup;
408: return $this[$name] = $control;
409: }
410:
411:
412:
413:
414:
415:
416:
417: 418: 419: 420: 421: 422:
423: final public function offsetSet($name, $component)
424: {
425: $this->addComponent($component, $name);
426: }
427:
428:
429:
430: 431: 432: 433: 434: 435:
436: final public function offsetGet($name)
437: {
438: return $this->getComponent($name, TRUE);
439: }
440:
441:
442:
443: 444: 445: 446: 447:
448: final public function offsetExists($name)
449: {
450: return $this->getComponent($name, FALSE) !== NULL;
451: }
452:
453:
454:
455: 456: 457: 458: 459:
460: final public function offsetUnset($name)
461: {
462: $component = $this->getComponent($name, FALSE);
463: if ($component !== NULL) {
464: $this->removeComponent($component);
465: }
466: }
467:
468:
469:
470: 471: 472:
473: final public function __clone()
474: {
475: throw new NotImplementedException('Form cloning is not supported yet.');
476: }
477:
478: }
479: