-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathForm.php
359 lines (319 loc) · 9.21 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Form;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventListenerInterface;
use Cake\Event\EventManager;
use Cake\Utility\Hash;
use Cake\Validation\ValidatorAwareInterface;
use Cake\Validation\ValidatorAwareTrait;
/**
* Form abstraction used to create forms not tied to ORM backed models,
* or to other permanent datastores. Ideal for implementing forms on top of
* API services, or contact forms.
*
* ### Building a form
*
* This class is most useful when subclassed. In a subclass you
* should define the `_buildSchema`, `validationDefault` and optionally,
* the `_execute` methods. These allow you to declare your form's
* fields, validation and primary action respectively.
*
* Forms are conventionally placed in the `App\Form` namespace.
*/
class Form implements EventListenerInterface, EventDispatcherInterface, ValidatorAwareInterface
{
use EventDispatcherTrait;
use ValidatorAwareTrait;
/**
* Name of default validation set.
*
* @var string
*/
public const DEFAULT_VALIDATOR = 'default';
/**
* The alias this object is assigned to validators as.
*
* @var string
*/
public const VALIDATOR_PROVIDER_NAME = 'form';
/**
* The name of the event dispatched when a validator has been built.
*
* @var string
*/
public const BUILD_VALIDATOR_EVENT = 'Form.buildValidator';
/**
* Schema class.
*
* @var string
* @psalm-var class-string<\Cake\Form\Schema>
*/
protected $_schemaClass = Schema::class;
/**
* The schema used by this form.
*
* @var \Cake\Form\Schema|null
*/
protected $_schema;
/**
* The errors if any
*
* @var array
*/
protected $_errors = [];
/**
* Form's data.
*
* @var array
*/
protected $_data = [];
/**
* Constructor
*
* @param \Cake\Event\EventManager|null $eventManager The event manager.
* Defaults to a new instance.
*/
public function __construct(?EventManager $eventManager = null)
{
if ($eventManager !== null) {
$this->setEventManager($eventManager);
}
$this->getEventManager()->on($this);
if (method_exists($this, '_buildValidator')) {
deprecationWarning(
static::class . ' implements `_buildValidator` which is no longer used. ' .
'You should implement `buildValidator(Validator $validator, string $name): void` ' .
'or `validationDefault(Validator $validator): Validator` instead.'
);
}
}
/**
* Get the Form callbacks this form is interested in.
*
* The conventional method map is:
*
* - Form.buildValidator => buildValidator
*
* @return array
*/
public function implementedEvents(): array
{
if (method_exists($this, 'buildValidator')) {
return [
self::BUILD_VALIDATOR_EVENT => 'buildValidator',
];
}
return [];
}
/**
* Set the schema for this form.
*
* @since 4.1.0
* @param \Cake\Form\Schema $schema The schema to set
* @return $this
*/
public function setSchema(Schema $schema)
{
$this->_schema = $schema;
return $this;
}
/**
* Get the schema for this form.
*
* This method will call `_buildSchema()` when the schema
* is first built. This hook method lets you configure the
* schema or load a pre-defined one.
*
* @since 4.1.0
* @return \Cake\Form\Schema the schema instance.
*/
public function getSchema(): Schema
{
if ($this->_schema === null) {
$this->_schema = $this->_buildSchema(new $this->_schemaClass());
}
return $this->_schema;
}
/**
* Get/Set the schema for this form.
*
* This method will call `_buildSchema()` when the schema
* is first built. This hook method lets you configure the
* schema or load a pre-defined one.
*
* @deprecated 4.1.0 Use {@link setSchema()}/{@link getSchema()} instead.
* @param \Cake\Form\Schema|null $schema The schema to set, or null.
* @return \Cake\Form\Schema the schema instance.
*/
public function schema(?Schema $schema = null): Schema
{
deprecationWarning('Form::schema() is deprecated. Use setSchema() and getSchema() instead.');
if ($schema !== null) {
$this->setSchema($schema);
}
return $this->getSchema();
}
/**
* A hook method intended to be implemented by subclasses.
*
* You can use this method to define the schema using
* the methods on Cake\Form\Schema, or loads a pre-defined
* schema from a concrete class.
*
* @param \Cake\Form\Schema $schema The schema to customize.
* @return \Cake\Form\Schema The schema to use.
*/
protected function _buildSchema(Schema $schema): Schema
{
return $schema;
}
/**
* Used to check if $data passes this form's validation.
*
* @param array $data The data to check.
* @return bool Whether or not the data is valid.
*/
public function validate(array $data): bool
{
$validator = $this->getValidator();
$this->_errors = $validator->validate($data);
return count($this->_errors) === 0;
}
/**
* Get the errors in the form
*
* Will return the errors from the last call
* to `validate()` or `execute()`.
*
* @return array Last set validation errors.
*/
public function getErrors(): array
{
return $this->_errors;
}
/**
* Set the errors in the form.
*
* ```
* $errors = [
* 'field_name' => ['rule_name' => 'message']
* ];
*
* $form->setErrors($errors);
* ```
*
* @param array $errors Errors list.
* @return $this
*/
public function setErrors(array $errors)
{
$this->_errors = $errors;
return $this;
}
/**
* Execute the form if it is valid.
*
* First validates the form, then calls the `_execute()` hook method.
* This hook method can be implemented in subclasses to perform
* the action of the form. This may be sending email, interacting
* with a remote API, or anything else you may need.
*
* @param array $data Form data.
* @return bool False on validation failure, otherwise returns the
* result of the `_execute()` method.
*/
public function execute(array $data): bool
{
$this->_data = $data;
if (!$this->validate($data)) {
return false;
}
return $this->_execute($data);
}
/**
* Hook method to be implemented in subclasses.
*
* Used by `execute()` to execute the form's action.
*
* @param array $data Form data.
* @return bool
*/
protected function _execute(array $data): bool
{
return true;
}
/**
* Get field data.
*
* @param string|null $field The field name or null to get data array with
* all fields.
* @return mixed
*/
public function getData(?string $field = null)
{
if ($field === null) {
return $this->_data;
}
return Hash::get($this->_data, $field);
}
/**
* Saves a variable or an associative array of variables for use inside form data.
*
* @param string|array $name The key to write, can be a dot notation value.
* Alternatively can be an array containing key(s) and value(s).
* @param mixed $value Value to set for var
* @return $this
*/
public function set($name, $value = null)
{
$write = $name;
if (!is_array($name)) {
$write = [$name => $value];
}
/** @psalm-suppress PossiblyInvalidIterator */
foreach ($write as $key => $val) {
$this->_data = Hash::insert($this->_data, $key, $val);
}
return $this;
}
/**
* Set form data.
*
* @param array $data Data array.
* @return $this
*/
public function setData(array $data)
{
$this->_data = $data;
return $this;
}
/**
* Get the printable version of a Form instance.
*
* @return array
*/
public function __debugInfo(): array
{
$special = [
'_schema' => $this->getSchema()->__debugInfo(),
'_errors' => $this->getErrors(),
'_validator' => $this->getValidator()->__debugInfo(),
];
return $special + get_object_vars($this);
}
}