Skip to content

Commit 2771363

Browse files
authored
fix(validation): deprecate string message for ValidationException constructor (#7005)
The ValidationException constructor currently accepts a string as the first argument. This is being deprecated in favor of accepting a ConstraintViolationListInterface. fixes #6997
1 parent 78293f5 commit 2771363

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Validator/Exception/ValidationException.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ class ValidationException extends RuntimeException implements ConstraintViolatio
8282
protected ?string $errorTitle = null;
8383
private array|ConstraintViolationListInterface $constraintViolationList = [];
8484

85-
public function __construct(ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
85+
public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
8686
{
8787
$this->errorTitle = $errorTitle;
88-
$this->constraintViolationList = $message;
89-
parent::__construct($this->__toString(), $code ?? 0, $previous);
88+
89+
if ($message instanceof ConstraintViolationListInterface) {
90+
$this->constraintViolationList = $message;
91+
parent::__construct($this->__toString(), $code ?? 0, $previous);
92+
93+
return;
94+
}
95+
96+
trigger_deprecation('api_platform/core', '5.0', \sprintf('The "%s" exception will have a "%s" first argument in 5.x.', self::class, ConstraintViolationListInterface::class));
97+
parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
9098
}
9199

92100
/**

0 commit comments

Comments
 (0)