|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Example; |
4 | 6 |
|
5 | 7 | require_once __DIR__ . '/../vendor/autoload.php'; |
6 | | - |
7 | 8 | use Symfony\Component\Validator\Constraints\Choice; |
8 | 9 | use Symftony\FormHandler\Exception\FormException; |
9 | 10 | use Symftony\FormHandler\Form\Extension\Invalid\Type\InvalidTypeExtension; |
|
14 | 15 | use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
15 | 16 | use Symfony\Component\Form\Forms; |
16 | 17 | use Symfony\Component\Validator\Validation; |
17 | | - |
18 | 18 | // Initialize form component |
19 | 19 | // add not submitted type extension |
20 | 20 | // add invalid type extension |
|
25 | 25 | ->addTypeExtension(new TransformationFailedTypeExtension()) |
26 | 26 | ->addExtension(new ValidatorExtension(Validation::createValidator())) |
27 | 27 | ->getFormFactory(); |
28 | | - |
29 | 28 | // Initialize form handler |
30 | 29 | $formHandler = new FormHandler(); |
31 | 30 | $formHandler->setFormFactory($formFactory); |
32 | | - |
33 | 31 | // Create the form |
34 | 32 | $form = $formHandler->createForm(ChoiceType::class, 'my-value', null, [ |
35 | 33 | 'method' => 'GET', |
|
46 | 44 | 'handler_not_submitted' => 'not submitted return data', |
47 | 45 | 'handler_transformation_failed' => 'transformation fail return data', |
48 | 46 | ]); |
49 | | - |
50 | 47 | // Handle request |
51 | 48 | $exception = null; |
52 | 49 | try { |
53 | 50 | $result = $formHandler->handleRequest($form); |
54 | | -} catch (FormException $e) { |
55 | | - $exception = $e; |
| 51 | +} catch (FormException $formException) { |
| 52 | + $exception = $formException; |
56 | 53 | } |
57 | 54 | ?> |
58 | 55 | <html> |
59 | 56 | <head> |
60 | 57 | <title>Form handler throw NotSubmitted/Invalid Exception example</title> |
61 | 58 | </head> |
62 | 59 | <body> |
63 | | -<?php include 'menu.php'; ?> |
| 60 | +<?php |
| 61 | +include __DIR__ . '/menu.php'; |
| 62 | +?> |
64 | 63 | <div class="container"> |
65 | 64 | <h1>Form handler will return default data when exception append</h1> |
66 | 65 | <div class="content"> |
|
85 | 84 | </form> |
86 | 85 |
|
87 | 86 | <div class="formdebug"> |
88 | | - <code>$form->isSubmitted() : <?= var_export($form->isSubmitted()) ?></code> |
89 | | - <code>$form->isValid() : <?= $form->isSubmitted() && var_export($form->isValid()) ?></code> |
| 87 | + <code>$form->isSubmitted() : <?php |
| 88 | +<?= var_export($form->isSubmitted()) ?> |
| 89 | +?></code> |
| 90 | + <code>$form->isValid() : <?php |
| 91 | +<?= $form->isSubmitted() && var_export($form->isValid()) ?> |
| 92 | +?></code> |
90 | 93 | <code>$form->getTransformationFailure() : |
91 | | - <?php if ($form->getTransformationFailure()): ?> |
| 94 | + <?php |
| 95 | +if ($form->getTransformationFailure() instanceof \Symfony\Component\Form\Exception\TransformationFailedException): ?> |
92 | 96 | <span class="warning"><?= $form->getTransformationFailure()->getMessage(); ?></span> |
93 | 97 | <?php else: ?> |
94 | 98 | null |
95 | 99 | <?php endif ?> |
| 100 | + |
| 101 | +?> |
96 | 102 | </code> |
97 | | - <code>$form->getData() : <?= var_export($form->getData()); ?></code> |
98 | | - <code>$formHandler->handleRequest($form) : <?php if (isset($result)) var_export($result); ?> |
99 | | - <?php if (null !== $exception): ?> |
| 103 | + <code>$form->getData() : <?php |
| 104 | +<?= var_export($form->getData()); |
| 105 | +?></code> |
| 106 | + <code>$formHandler->handleRequest($form) : <?php |
| 107 | +if (isset($result)) { |
| 108 | + var_export($result); |
| 109 | +} |
| 110 | + |
| 111 | +?> |
| 112 | + <?php |
| 113 | +if ($exception instanceof \Symftony\FormHandler\Exception\FormException): ?> |
100 | 114 | <span class="error"><?= get_class($exception) . ' : ' . $exception->getMessage() ?></span> |
101 | 115 | <?php endif ?> |
| 116 | + |
| 117 | +?> |
102 | 118 | </code> |
103 | 119 | </div> |
104 | 120 | </div> |
105 | 121 | </div> |
106 | 122 | </body> |
107 | 123 | </html> |
| 124 | +<?php |
0 commit comments