1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\FormsLatte;
9:
10: use Nette;
11: use Nette\Forms\Form;
12: use Nette\Utils\Html;
13:
14:
15: 16: 17: 18:
19: class Runtime
20: {
21: use Nette\StaticClass;
22:
23: 24: 25: 26:
27: public static function renderFormBegin(Form $form, array $attrs, $withTags = true)
28: {
29: $form->fireRenderEvents();
30: foreach ($form->getControls() as $control) {
31: $control->setOption('rendered', false);
32: }
33: $el = $form->getElementPrototype();
34: $el->action = (string) $el->action;
35: $el = clone $el;
36: if ($form->isMethod('get')) {
37: $el->action = preg_replace('~\?[^#]*~', '', $el->action, 1);
38: }
39: $el->addAttributes($attrs);
40: return $withTags ? $el->startTag() : $el->attributes();
41: }
42:
43:
44: 45: 46: 47:
48: public static function renderFormEnd(Form $form, $withTags = true)
49: {
50: $s = '';
51: if ($form->isMethod('get')) {
52: foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
53: $parts = explode('=', $param, 2);
54: $name = urldecode($parts[0]);
55: $prefix = explode('[', $name, 2)[0];
56: if (!isset($form[$prefix])) {
57: $s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])]);
58: }
59: }
60: }
61:
62: foreach ($form->getControls() as $control) {
63: if ($control->getOption('type') === 'hidden' && !$control->getOption('rendered')) {
64: $s .= $control->getControl();
65: }
66: }
67:
68: if (iterator_count($form->getComponents(true, Nette\Forms\Controls\TextInput::class)) < 2) {
69: $s .= "<!--[if IE]><input type=IEbug disabled style=\"display:none\"><![endif]-->\n";
70: }
71:
72: return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : '');
73: }
74: }
75: