Skip to content

Commit d6adeb3

Browse files
committed
perf: defer instantiation of validation
1 parent 52c2222 commit d6adeb3

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

system/BaseModel.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ abstract class BaseModel
199199
/**
200200
* Our validator instance.
201201
*
202-
* @var ValidationInterface
202+
* @var ValidationInterface|null
203203
*/
204204
protected $validation;
205205

@@ -326,10 +326,6 @@ public function __construct(?ValidationInterface $validation = null)
326326
$this->tempUseSoftDeletes = $this->useSoftDeletes;
327327
$this->tempAllowCallbacks = $this->allowCallbacks;
328328

329-
/**
330-
* @var ValidationInterface|null $validation
331-
*/
332-
$validation ??= Services::validation(null, false);
333329
$this->validation = $validation;
334330

335331
$this->initialize();
@@ -1153,6 +1149,10 @@ public function replace(?array $data = null, bool $returnSQL = false)
11531149
*/
11541150
public function errors(bool $forceDB = false)
11551151
{
1152+
if ($this->validation === null) {
1153+
return $this->doErrors();
1154+
}
1155+
11561156
// Do we have validation errors?
11571157
if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors())) {
11581158
return $errors;
@@ -1421,6 +1421,8 @@ public function setValidationRule(string $field, $fieldRules)
14211421
// ValidationRules can be either a string, which is the group name,
14221422
// or an array of rules.
14231423
if (is_string($rules)) {
1424+
$this->ensureValidation();
1425+
14241426
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
14251427

14261428
$this->validationRules = $rules;
@@ -1455,9 +1457,13 @@ public function cleanRules(bool $choice = false)
14551457
*/
14561458
public function validate($data): bool
14571459
{
1460+
if ($this->skipValidation || empty($data)) {
1461+
return true;
1462+
}
1463+
14581464
$rules = $this->getValidationRules();
14591465

1460-
if ($this->skipValidation || empty($rules) || empty($data)) {
1466+
if (empty($rules)) {
14611467
return true;
14621468
}
14631469

@@ -1474,6 +1480,8 @@ public function validate($data): bool
14741480
return true;
14751481
}
14761482

1483+
$this->ensureValidation();
1484+
14771485
$this->validation->reset()->setRules($rules, $this->validationMessages);
14781486

14791487
return $this->validation->run($data, null, $this->DBGroup);
@@ -1492,6 +1500,8 @@ public function getValidationRules(array $options = []): array
14921500
// ValidationRules can be either a string, which is the group name,
14931501
// or an array of rules.
14941502
if (is_string($rules)) {
1503+
$this->ensureValidation();
1504+
14951505
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
14961506

14971507
$this->validationMessages += $customErrors;
@@ -1506,6 +1516,13 @@ public function getValidationRules(array $options = []): array
15061516
return $rules;
15071517
}
15081518

1519+
protected function ensureValidation(): void
1520+
{
1521+
if ($this->validation === null) {
1522+
$this->validation = Services::validation(null, false);
1523+
}
1524+
}
1525+
15091526
/**
15101527
* Returns the model's validation messages, so they
15111528
* can be used elsewhere, if needed.

0 commit comments

Comments
 (0)