Skip to content

Commit

Permalink
NEW Show more information in ValidationException message (#11562)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Feb 10, 2025
2 parents 873d552 + 8214e31 commit 2856b26
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 221 deletions.
3 changes: 1 addition & 2 deletions src/Core/Validation/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
class ConstraintValidator
{
/**
* Validate a value by a constraint
* Validate a value by a constraint or an array of constraints
*
* @param Constraint|Constraint[] $constraints a constraint or array of constraints to validate against
* @param string $fieldName The field name the value relates to, if relevant
*/
public static function validate(mixed $value, Constraint|array $constraints, string $fieldName = ''): ValidationResult
{
Expand Down
44 changes: 44 additions & 0 deletions src/Core/Validation/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Exception;
use InvalidArgumentException;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Control\Director;
use SilverStripe\Dev\DevelopmentAdmin;
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Configurable;

/**
* Exception thrown by {@link DataObject}::write if validation fails. By throwing an
Expand All @@ -14,6 +19,15 @@
class ValidationException extends Exception
{
use Injectable;
use Configurable;

/**
* List of controllers to show additioanl info when not in CLI
* Subclasses of these controllers will also show additional info
*/
private static array $show_additional_info_non_cli_controllers = [
DevelopmentAdmin::class
];

/**
* The contained ValidationResult related to this error
Expand Down Expand Up @@ -48,6 +62,16 @@ public function __construct($result = null, $code = 0)
// Pick first message
foreach ($result->getMessages() as $message) {
$exceptionMessage = $message['message'];
if ($this->doShowAdditionalInfo()) {
if ($message['fieldName']) {
$exceptionMessage .= ' - fieldName: ' . $message['fieldName'];
}
$dataClass = $result->getModelClass();
if (is_subclass_of($dataClass, DataObject::class, true)) {
$exceptionMessage .= ', recordID: ' . $result->getRecordID();
$exceptionMessage .= ', dataClass: ' . $dataClass;
}
}
break;
}
} elseif (is_string($result)) {
Expand All @@ -71,4 +95,24 @@ public function getResult()
{
return $this->result;
}

/**
* Whether to show additional information in the error message depending on the context
*
* We do not want this to show this in a context where it's easy to know the record and value that
* triggered the error e.g. form submission, API calls, etc
*/
private function doShowAdditionalInfo(): bool
{
if (Director::is_cli()) {
return true;
}
$currentController = Controller::curr();
foreach (static::config()->get('show_additional_info_non_cli_controllers') as $controller) {
if (is_a($currentController, $controller)) {
return true;
}
}
return false;
}
}
Loading

0 comments on commit 2856b26

Please sign in to comment.