1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2021-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Util\Forms;
24:
25: use Psr\EventDispatcher\StoppableEventInterface;
26: use SimpleID\Util\Events\BaseEvent;
27: use SimpleID\Util\Events\GenericEventTrait;
28: use SimpleID\Util\Events\StoppableEventTrait;
29:
30: /**
31: * A generic event used to process a submitted form. This includes
32: * validating whether the data submitted in a form is valid as well as
33: * processing submitted data.
34: *
35: * If a validation error is encountered, call {@link setInvalid()}. Validation
36: * messages can be added using {@link addMessage()}.
37: *
38: * This event implements `StoppableEventInterface`, which stops further
39: * processing of the form. The documentation of the specific event should
40: * specify when processing should be stopped - an invalid validation
41: * result is not sufficient reason for further processing to be stopped.
42: *
43: */
44: class FormSubmitEvent extends BaseEvent implements \GenericEventInterface, StoppableEventInterface, FormEventInterface {
45: use GenericEventTrait;
46: use StoppableEventTrait;
47:
48: /** @var \SimpleID\Util\Forms\FormState */
49: protected $form_state;
50: /** @var bool */
51: protected $is_valid = true;
52: /** @var array<string> */
53: protected $messages = [];
54:
55:
56: /**
57: * Creates a form submission event.
58: *
59: * @param \SimpleID\Util\Forms\FormState $form_state a reference to the form state array
60: * @param string $eventName the name of the event, or the name
61: */
62: public function __construct($form_state, $eventName = null) {
63: $this->setEventName($eventName);
64: $this->form_state = $form_state;
65: }
66:
67: /**
68: * {@inheritdoc}
69: */
70: public function getFormState() {
71: return $this->form_state;
72: }
73:
74: /**
75: * Adds a validation error message
76: *
77: * @param string $message the error message
78: * @return void
79: */
80: public function addMessage($message) {
81: $this->messages[] = $message;
82: }
83:
84: /**
85: * Returns a list of error messages
86: *
87: * @return array<string> the error messages
88: */
89: public function getMessages() {
90: return $this->messages;
91: }
92:
93: /**
94: * Sets the form validation result as invalid.
95: *
96: * @return void
97: */
98: public function setInvalid() {
99: $this->is_valid = false;
100: }
101:
102: /**
103: * Returns whether the form has been validated.
104: *
105: * @return bool true if the form is valid
106: */
107: public function isValid() {
108: return $this->is_valid;
109: }
110: }
111:
112: ?>