|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Flow; |
| 13 | + |
| 14 | +use Symfony\Component\Form\FormInterface; |
| 15 | +use Symfony\Component\Form\SubmitButton; |
| 16 | + |
| 17 | +/** |
| 18 | + * A button that submits the form and handles an action. |
| 19 | + * |
| 20 | + * @author Yonel Ceruto <[email protected]> |
| 21 | + */ |
| 22 | +class ButtonFlow extends SubmitButton implements ButtonFlowInterface |
| 23 | +{ |
| 24 | + private mixed $data = null; |
| 25 | + private bool $handled = false; |
| 26 | + |
| 27 | + public function submit(array|string|null $submittedData, bool $clearMissing = true): static |
| 28 | + { |
| 29 | + if ($this->isSubmitted()) { |
| 30 | + return $this; // ignore double submit |
| 31 | + } |
| 32 | + |
| 33 | + parent::submit($submittedData, $clearMissing); |
| 34 | + |
| 35 | + if ($this->isSubmitted()) { |
| 36 | + $this->data = $submittedData; |
| 37 | + } |
| 38 | + |
| 39 | + return $this; |
| 40 | + } |
| 41 | + |
| 42 | + public function getViewData(): mixed |
| 43 | + { |
| 44 | + return $this->data; |
| 45 | + } |
| 46 | + |
| 47 | + public function handle(): void |
| 48 | + { |
| 49 | + /** @var FormInterface $form */ |
| 50 | + $form = $this->getParent(); |
| 51 | + $data = $form->getData(); |
| 52 | + |
| 53 | + while ($form && !$form instanceof FormFlowInterface) { |
| 54 | + $form = $form->getParent(); |
| 55 | + } |
| 56 | + |
| 57 | + $handler = $this->getConfig()->getOption('handler'); |
| 58 | + $handler($data, $this, $form); |
| 59 | + |
| 60 | + $this->handled = true; |
| 61 | + } |
| 62 | + |
| 63 | + public function isHandled(): bool |
| 64 | + { |
| 65 | + return $this->handled; |
| 66 | + } |
| 67 | + |
| 68 | + public function isResetAction(): bool |
| 69 | + { |
| 70 | + return 'reset' === $this->getConfig()->getAttribute('action'); |
| 71 | + } |
| 72 | + |
| 73 | + public function isPreviousAction(): bool |
| 74 | + { |
| 75 | + return 'previous' === $this->getConfig()->getAttribute('action'); |
| 76 | + } |
| 77 | + |
| 78 | + public function isNextAction(): bool |
| 79 | + { |
| 80 | + return 'next' === $this->getConfig()->getAttribute('action'); |
| 81 | + } |
| 82 | + |
| 83 | + public function isFinishAction(): bool |
| 84 | + { |
| 85 | + return 'finish' === $this->getConfig()->getAttribute('action'); |
| 86 | + } |
| 87 | + |
| 88 | + public function isClearSubmission(): bool |
| 89 | + { |
| 90 | + return $this->getConfig()->getOption('clear_submission'); |
| 91 | + } |
| 92 | +} |
0 commit comments