Skip to content

Commit

Permalink
Add event to make model validation extendable (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin authored Oct 16, 2023
1 parent 5a3ffdc commit c0acc2b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,27 @@ public function setValidationAttributeName($attr, $name)
*/
protected function getValidationAttributes()
{
return $this->getAttributes();
$attributes = $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));
* }
* });
*
*/
if (($validationAttributes = $this->fireEvent('model.getValidationAttributes', [$attributes], true)) !== null) {
return $validationAttributes;
}

return $attributes;
}

/**
Expand Down

0 comments on commit c0acc2b

Please sign in to comment.