Unverified Commit 20496dd2 authored by alexpott's avatar alexpott

Issue #3029540 by mikelutz, catch, alexpott, larowlan, Gábor Hojtsy: [Symfony...

Issue #3029540 by mikelutz, catch, alexpott, larowlan, Gábor Hojtsy: [Symfony 4] Sub class \Symfony\Component\Validator\ConstraintViolation and use that in \Drupal\Core\TypedData\Validation\ExecutionContext::addViolation()
parent 202ad58e
......@@ -8,7 +8,7 @@
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Drupal\Core\Validation\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
......
......@@ -5,7 +5,7 @@
use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\Validator\ConstraintViolation;
use Drupal\Core\Validation\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationList;
......
......@@ -2,9 +2,9 @@
namespace Drupal\Core\TypedData\Validation;
use Drupal\Core\Validation\ConstraintViolation;
use Drupal\Core\Validation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
......
......@@ -2,9 +2,9 @@
namespace Drupal\Core\TypedData\Validation;
use Drupal\Core\Validation\ConstraintViolation;
use Drupal\Core\Validation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
......
<?php
namespace Drupal\Core\Validation;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation;
/**
* ConstraintViolation subclass to handle markup in messages.
*
* In symfony 4, the message in a violation is typecast to string. This class
* allows for a markup object to be used instead.
*/
class ConstraintViolation extends SymfonyConstraintViolation {
/**
* The violation message, may be markup or a string.
*
* @var \Drupal\Component\Render\MarkupInterface|string
*/
private $originalMessage;
/**
* Constructs a ConstraintViolation object.
*
* @param \Drupal\Component\Render\MarkupInterface|string $message
* The violation message.
* @param string $messageTemplate
* The raw violation message.
* @param array $parameters
* The parameters to substitute in the raw violation message.
* @param mixed $root
* The value originally passed to the validator.
* @param string|null $propertyPath
* The property path from the root value to the invalid value.
* @param mixed $invalidValue
* The invalid value that caused this violation.
* @param int|null $plural
* The number for determining the plural form when translating the message.
* @param mixed $code
* The error code of the violation.
* @param \Symfony\Component\Validator\Constraint|null $constraint
* The constraint whose validation caused the violation.
* @param mixed $cause
* The cause of the violation.
*/
public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = NULL, $code = NULL, Constraint $constraint = NULL, $cause = NULL) {
$this->originalMessage = $message;
parent::__construct($message, $messageTemplate, $parameters, $root, $propertyPath, $invalidValue, $plural, $code, $constraint, $cause);
}
/**
* Returns the violation message.
*
* @return \Drupal\Component\Render\MarkupInterface|string
* The violation message
*/
public function getMessage() {
return $this->originalMessage;
}
}
......@@ -25,7 +25,7 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\ConstraintViolation;
use Drupal\Core\Validation\ConstraintViolation;
/**
* Reads data from an upload stream and creates a corresponding file entity.
......
......@@ -7,8 +7,8 @@
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Validation\ConstraintViolation;
use Drupal\link\LinkItemInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
......
......@@ -6,8 +6,8 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_test\Plugin\Validation\Constraint\EntityTestCompositeConstraint;
use Drupal\Core\Validation\ConstraintViolation;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Validator\ConstraintViolation;
/**
* @coversDefaultClass \Drupal\Core\Entity\EntityConstraintViolationList
......
<?php
namespace Drupal\Tests\Core\Validation;
use Drupal\Core\Render\Markup;
use Drupal\Core\Validation\ConstraintViolation;
use Drupal\Tests\UnitTestCase;
/**
* Tests the ConstraintViolation class.
*
* @group Validation
*
* @coversDefaultClass \Drupal\Core\Validation\ConstraintViolation
*/
class ConstraintViolationTest extends UnitTestCase {
/**
* Tests that the getMessage method returns the original markup.
*
* @covers ::getMessage
*/
public function testGetMessage() {
$markup = Markup::create($this->getRandomGenerator()->string(32));
$violation = new ConstraintViolation($markup, (string) $markup, [], 'TestClass', 'test', 0);
$this->assertSame($markup, $violation->getMessage());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment