Skip to content

Commit e660250

Browse files
committed
deprecate Form::_execute
1 parent af468b0 commit e660250

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

src/Form/Form.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Cake\Utility\Hash;
2424
use Cake\Validation\ValidatorAwareInterface;
2525
use Cake\Validation\ValidatorAwareTrait;
26+
use ReflectionMethod;
27+
use function Cake\Core\deprecationWarning;
2628

2729
/**
2830
* Form abstraction used to create forms not tied to ORM backed models,
@@ -33,7 +35,7 @@
3335
*
3436
* This class is most useful when subclassed. In a subclass you
3537
* should define the `_buildSchema`, `validationDefault` and optionally,
36-
* the `_execute` methods. These allow you to declare your form's
38+
* the `process` methods. These allow you to declare your form's
3739
* fields, validation and primary action respectively.
3840
*
3941
* Forms are conventionally placed in the `App\Form` namespace.
@@ -243,7 +245,7 @@ public function setErrors(array $errors)
243245
/**
244246
* Execute the form if it is valid.
245247
*
246-
* First validates the form, then calls the `_execute()` hook method.
248+
* First validates the form, then calls the `process()` hook method.
247249
* This hook method can be implemented in subclasses to perform
248250
* the action of the form. This may be sending email, interacting
249251
* with a remote API, or anything else you may need.
@@ -256,21 +258,45 @@ public function setErrors(array $errors)
256258
* @param array $data Form data.
257259
* @param array<string, mixed> $options List of options.
258260
* @return bool False on validation failure, otherwise returns the
259-
* result of the `_execute()` method.
261+
* result of the `process()` method.
260262
*/
261263
public function execute(array $data, array $options = []): bool
262264
{
263-
$this->_data = $data;
265+
// check for deprecated _execute() method - https://github.com/cakephp/cakephp/pull/18725
266+
$childClass = static::class;
267+
$parentClass = self::class;
268+
$method = new ReflectionMethod($childClass, '_execute');
269+
$hasOverwrittenExecute = $method->getDeclaringClass()->getName() !== $parentClass;
264270

271+
$this->_data = $data;
265272
$options += ['validate' => true];
266273

267274
if ($options['validate'] === false) {
268-
return $this->_execute($data);
275+
if ($hasOverwrittenExecute) {
276+
deprecationWarning(
277+
'5.3.0',
278+
'The _execute() method is deprecated. Override the process() method instead.',
279+
);
280+
281+
return $this->_execute($data);
282+
}
283+
284+
return $this->process($data);
269285
}
270286

271287
$validator = $options['validate'] === true ? static::DEFAULT_VALIDATOR : $options['validate'];
288+
$validateResult = $this->validate($data, $validator);
289+
290+
if ($hasOverwrittenExecute) {
291+
deprecationWarning(
292+
'5.3.0',
293+
'The _execute() method is deprecated. Override the process() method instead.',
294+
);
272295

273-
return $this->validate($data, $validator) && $this->_execute($data);
296+
return $validateResult && $this->_execute($data);
297+
}
298+
299+
return $validateResult && $this->process($data);
274300
}
275301

276302
/**
@@ -282,6 +308,19 @@ public function execute(array $data, array $options = []): bool
282308
* @return bool
283309
*/
284310
protected function _execute(array $data): bool
311+
{
312+
return $this->process($data);
313+
}
314+
315+
/**
316+
* Hook method to be implemented in subclasses.
317+
*
318+
* Used by `execute()` to execute the form's action.
319+
*
320+
* @param array $data Form data.
321+
* @return bool
322+
*/
323+
protected function process(array $data): bool
285324
{
286325
return true;
287326
}

tests/TestCase/Form/FormTest.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,22 @@ public function testSetErrors(): void
165165
*/
166166
public function testExecuteInvalid(): void
167167
{
168-
$form = new class extends Form {
169-
// phpcs:ignore CakePHP.NamingConventions.ValidFunctionName.PublicWithUnderscore
170-
public function _execute(array $data): bool
171-
{
172-
throw new Exception('Should not be called');
173-
}
174-
};
175-
$form->getValidator()
176-
->add('email', 'format', ['rule' => 'email']);
177-
$data = [
178-
'email' => 'rong',
179-
];
180-
181-
$this->assertFalse($form->execute($data));
168+
$this->deprecated(function (): void {
169+
$form = new class extends Form {
170+
// phpcs:ignore CakePHP.NamingConventions.ValidFunctionName.PublicWithUnderscore
171+
public function _execute(array $data): bool
172+
{
173+
throw new Exception('Should not be called');
174+
}
175+
};
176+
$form->getValidator()
177+
->add('email', 'format', ['rule' => 'email']);
178+
$data = [
179+
'email' => 'rong',
180+
];
181+
182+
$this->assertFalse($form->execute($data));
183+
});
182184
}
183185

184186
/**
@@ -196,6 +198,21 @@ public function testExecuteValid(): void
196198
$this->assertTrue($form->execute($data));
197199
}
198200

201+
/**
202+
* test execute() when data is valid.
203+
*/
204+
public function testExecuteWithProcess(): void
205+
{
206+
$form = new class extends Form {
207+
public function process(array $data): bool
208+
{
209+
return false;
210+
}
211+
};
212+
213+
$this->assertFalse($form->execute([]));
214+
}
215+
199216
/**
200217
* test execute() when data is valid.
201218
*/

0 commit comments

Comments
 (0)