From 4ff3c18153a1cf3660f5299756cc48e8fe86a18d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 27 Jan 2023 22:46:26 -0500 Subject: [PATCH] add event to make model validation extendable --- src/Database/Traits/Validation.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index 94dca33d1..2467b1769 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -111,6 +111,25 @@ public function setValidationAttributeName($attr, $name) */ protected function getValidationAttributes() { + /** + * @event model.getValidationAttributes + * Called when fetching the model attributes to validate the model + * + * Example usage from TranslatableBehavior class: + * + * $model->bindEvent('model.getValidationAttributes', function() { + * $locale = $this->translateContext(); + * if ($locale !== $this->translatableDefault) { + * $attributes = $this->model->getAttributes(); + * return array_merge($attributes, $this->getTranslateDirty($locale)); + * } + * }); + * + */ + if ($attributes = $this->fireEvent('model.getValidationAttributes', [], true)) { + return $attributes; + } + return $this->getAttributes(); }