Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event to make model validation extendable #139

Merged
merged 7 commits into from
Oct 16, 2023
22 changes: 21 additions & 1 deletion src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,27 @@ public function setValidationAttributeName($attr, $name)
*/
protected function getValidationAttributes()
{
return $this->getAttributes();
/**
* @event model.getValidationAttributes
* Called when fetching the model attributes to validate the model
*
* Example usage from TranslatableBehavior class:
*
* $model->bindEvent('model.getValidationAttributes', function ($attributes) {
* $locale = $this->translateContext();
* if ($locale !== $this->translatableDefault) {
* return array_merge($attributes, $this->getTranslateDirty($locale));
* }
* });
*
*/

$attributes = $this->getAttributes();
if (($validationAttributes = $this->fireEvent('model.getValidationAttributes', [$attributes], true)) !== null) {
return $validationAttributes;
}
mjauvin marked this conversation as resolved.
Show resolved Hide resolved

return $attributes;
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down