|
9 | 9 |
|
10 | 10 | namespace Nette\Bridges\FormsLatte; |
11 | 11 |
|
| 12 | +use Latte; |
12 | 13 | use Nette; |
13 | 14 | use Nette\Forms\Form; |
14 | 15 | use Nette\Utils\Html; |
@@ -71,4 +72,85 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string |
71 | 72 |
|
72 | 73 | return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : ''); |
73 | 74 | } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Generates blueprint of form. |
| 79 | + */ |
| 80 | + public static function renderBlueprint($form): void |
| 81 | + { |
| 82 | + $dummyForm = new Form; |
| 83 | + $dict = new \SplObjectStorage; |
| 84 | + foreach ($form->getControls() as $name => $input) { |
| 85 | + $dict[$input] = $dummyInput = new class extends Nette\Forms\Controls\BaseControl { |
| 86 | + public $inner; |
| 87 | + |
| 88 | + |
| 89 | + public function getLabel($name = null) |
| 90 | + { |
| 91 | + return $this->inner->getLabel() ? '{label ' . $this->inner->lookupPath(Form::class) . '}' : null; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public function getControl() |
| 96 | + { |
| 97 | + return '{input ' . $this->inner->lookupPath(Form::class) . '}'; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public function isRequired(): bool |
| 102 | + { |
| 103 | + return $this->inner->isRequired(); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + public function getOption($key, $default = null) |
| 108 | + { |
| 109 | + return $key === 'rendered' ? parent::getOption($key) : $this->inner->getOption($key, $default); |
| 110 | + } |
| 111 | + }; |
| 112 | + $dummyInput->inner = $input; |
| 113 | + $dummyForm->addComponent($dummyInput, (string) $dict->count()); |
| 114 | + $dummyInput->addError('{inputError ' . $input->lookupPath(Form::class) . '}'); |
| 115 | + } |
| 116 | + |
| 117 | + foreach ($form->getGroups() as $group) { |
| 118 | + $dummyGroup = $dummyForm->addGroup(); |
| 119 | + foreach ($group->getOptions() as $k => $v) { |
| 120 | + $dummyGroup->setOption($k, $v); |
| 121 | + } |
| 122 | + foreach ($group->getControls() as $control) { |
| 123 | + if ($dict[$control]) { |
| 124 | + $dummyGroup->add($dict[$control]); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + $renderer = clone $form->getRenderer(); |
| 130 | + $dummyForm->setRenderer($renderer); |
| 131 | + |
| 132 | + if ($renderer instanceof Nette\Forms\Rendering\DefaultFormRenderer) { |
| 133 | + $renderer->wrappers['error']['container'] = $renderer->getWrapper('error container')->setAttribute('n:ifcontent', true); |
| 134 | + $renderer->wrappers['error']['item'] = $renderer->getWrapper('error item')->setAttribute('n:foreach', '$form->getOwnErrors() as $error'); |
| 135 | + $renderer->wrappers['control']['errorcontainer'] = $renderer->getWrapper('control errorcontainer')->setAttribute('n:ifcontent', true); |
| 136 | + $dummyForm->addError('{$error}'); |
| 137 | + |
| 138 | + ob_start(); |
| 139 | + $dummyForm->render('end'); |
| 140 | + $end = ob_get_clean(); |
| 141 | + } |
| 142 | + |
| 143 | + ob_start(); |
| 144 | + $dummyForm->render(); |
| 145 | + $body = ob_get_clean(); |
| 146 | + |
| 147 | + $body = str_replace($dummyForm->getElementPrototype()->startTag(), '<form n:name="' . $form->getName() . '">', $body); |
| 148 | + $body = str_replace($end ?? '', '</form>', $body); |
| 149 | + |
| 150 | + $blueprint = new Latte\Runtime\Blueprint; |
| 151 | + $end = $blueprint->printCanvas(); |
| 152 | + $blueprint->printHeader('Form ' . $form->getName()); |
| 153 | + $blueprint->printCode($body); |
| 154 | + echo $end; |
| 155 | + } |
74 | 156 | } |
0 commit comments