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