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

NEW Show more information in ValidationException message #11562

Open
wants to merge 1 commit into
base: 6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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 ($this->config()->get('show_additional_info_non_cli_controllers') as $controller) {
if (is_a($currentController, $controller)) {
return true;
}
}
return false;
}
}
Loading
Loading