|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +use Nette\Bridges\FormsLatte\FormsExtension; |
| 4 | +use Nette\Forms\Form; |
| 5 | +use Tester\Assert; |
| 6 | + |
| 7 | +require __DIR__ . '/../bootstrap.php'; |
| 8 | + |
| 9 | + |
| 10 | +$form = new Form('myForm'); |
| 11 | +$form->addHidden('id', '42'); |
| 12 | +$form->addText('name', 'Name:'); |
| 13 | +$form->addText('email', 'Email:'); |
| 14 | +$form->addSubmit('submit', 'Save'); |
| 15 | + |
| 16 | +$latte = new Latte\Engine; |
| 17 | +$latte->addExtension(new FormsExtension); |
| 18 | +$latte->addProvider('uiControl', ['myForm' => $form]); |
| 19 | + |
| 20 | +Assert::matchFile( |
| 21 | + __DIR__ . '/expected/forms.detached.php', |
| 22 | + $latte->compile(__DIR__ . '/templates/forms.detached.latte'), |
| 23 | +); |
| 24 | + |
| 25 | +Assert::matchFile( |
| 26 | + __DIR__ . '/expected/forms.detached.html', |
| 27 | + $latte->renderToString(__DIR__ . '/templates/forms.detached.latte'), |
| 28 | +); |
| 29 | + |
| 30 | + |
| 31 | +test('detached requires a Form instance, not a plain Container', function () { |
| 32 | + $container = new Nette\Forms\Container; |
| 33 | + $container->addText('name'); |
| 34 | + $latte = new Latte\Engine; |
| 35 | + $latte->setLoader(new Latte\Loaders\StringLoader); |
| 36 | + $latte->addExtension(new FormsExtension); |
| 37 | + $latte->addProvider('uiControl', ['myCont' => $container]); |
| 38 | + |
| 39 | + Assert::exception( |
| 40 | + fn() => $latte->renderToString("{form detached myCont}{/form}\n"), |
| 41 | + Nette\InvalidStateException::class, |
| 42 | + 'Detached mode requires a Form instance.', |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | + |
| 47 | +test('detached form must have an id', function () { |
| 48 | + $form = new Nette\Forms\Form; // no name → no id |
| 49 | + $form->addText('name'); |
| 50 | + $latte = new Latte\Engine; |
| 51 | + $latte->setLoader(new Latte\Loaders\StringLoader); |
| 52 | + $latte->addExtension(new FormsExtension); |
| 53 | + $latte->addProvider('uiControl', ['noName' => $form]); |
| 54 | + |
| 55 | + Assert::exception( |
| 56 | + fn() => $latte->renderToString("{form detached noName}{/form}\n"), |
| 57 | + Nette\InvalidStateException::class, |
| 58 | + 'Detached form must have an id%a%', |
| 59 | + ); |
| 60 | +}); |
0 commit comments