Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?php namespace Watson\Validating; | ||
|
||
use \Illuminate\Database\Eloquent\Model; | ||
use \Illuminate\Support\Facades\Event; | ||
use \Watson\Validating\ValidationException; | ||
|
||
class ValidatingObserver { | ||
|
@@ -74,20 +75,42 @@ public function restoring(Model $model) | |
protected function performValidation(Model $model, $event) | ||
{ | ||
// If the model has validating enabled, perform it. | ||
if ($model->getValidating() && $model->isValid($event) === false) | ||
if ($model->getValidating()) | ||
{ | ||
if ($model->getThrowValidationExceptions()) | ||
// Fire the namespaced validating event and prevent validation | ||
// if it returns a value. | ||
if ($this->fireValidatingEvent($event, $model) !== null) return; | ||
|
||
if ($model->isValid($event) === false) | ||
{ | ||
$exception = new ValidationException(get_class($model) . ' model could not be persisted as it failed validation.'); | ||
// Fire the validating.failed event. | ||
$this->fireValidatedEvent('failed', $model); | ||
|
||
$exception->setModel($model); | ||
$exception->setErrors($model->getErrors()); | ||
if ($model->getThrowValidationExceptions()) | ||
{ | ||
$exception = new ValidationException(get_class($model) . ' model could not be persisted as it failed validation.'); | ||
|
||
throw $exception; | ||
} | ||
$exception->setModel($model); | ||
$exception->setErrors($model->getErrors()); | ||
|
||
throw $exception; | ||
} | ||
|
||
return false; | ||
return false; | ||
} | ||
// Fire the validating.passed event. | ||
$this->fireValidatedEvent('passed', $model); | ||
} | ||
} | ||
|
||
protected function fireValidatingEvent($event, Model $model) | ||
{ | ||
return Event::until('validating.'.$event, [$model]); | ||
} | ||
|
||
protected function fireValidatedEvent($event, Model $model) | ||
{ | ||
return Event::fire('validating.'.$event, [$model]); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dwightwatson
Author
Owner
|
||
} | ||
|
||
} |
To be more semantically friendly with Laravel, this should be
validated.{event}
. That event should always fire before leaving theif
statement in theperformValidation()
method just likevalidating.{event}
fires just as you leave theif
statement. I think you should also firevalidation.passed
orvalidation.failed
based on the response. You might also firevalidation.skipped
as theelse
part of theif
statement inperformValidation()
. That would allow for the most flexibility in wildcard listeners and plenty of event hooks:validation.skipped
(always when disabled: only event fired)or
validating.{event}
(before validation, always when enabled)validation.passed
(returns true) orvalidation.failed
(returns false)validated.{event}
(after validation, always when enabled)But you know better what you're trying to accomplish here.