Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Merge 18cfa5f into 6cb12b6
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Apr 21, 2019
2 parents 6cb12b6 + 18cfa5f commit 19c7c7c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/reference/conditional_validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ object. The object can be used to check assertions against the model::

// ...

/* Specify the translation domain */
$errorElement
->with('value')
->addViolation('translation_key_with_{var}', ['%var%' => 'value'], null, 'translation_domain')
->end();

// ...

/* conditional validation */
if ($this->getSubject()->getState() == Post::STATUS_ONLINE) {
$errorElement
Expand Down
33 changes: 28 additions & 5 deletions src/Form/Validator/ErrorElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

final class ErrorElement
{
private const DEFAULT_TRANSLATION_DOMAIN = 'validators';

/**
* @var ExecutionContextInterface
*/
Expand Down Expand Up @@ -146,6 +148,11 @@ public function getSubject()

/**
* @param string|array $message
* @param array $parameters
* @param null $value
* @param string $translationDomain
*
* @return ErrorElement
*/
public function addViolation($message, array $parameters = [], $value = null): self
{
Expand All @@ -155,11 +162,27 @@ public function addViolation($message, array $parameters = [], $value = null): s
$message = $message[0] ?? 'error';
}

$this->context->buildViolation($message)
->atPath((string) $this->getCurrentPropertyPath())
->setParameters($parameters)
->setInvalidValue($value)
->addViolation();
$subPath = (string) $this->getCurrentPropertyPath();
$translationDomain = self::DEFAULT_TRANSLATION_DOMAIN;

// NEXT_MAJOR: Remove this hack
if (\func_num_args() >= 4) {
$arg = func_get_arg(3);
if ((\is_string($arg))) {
$translationDomain = $arg;
}
}

if ($this->context instanceof LegacyExecutionContextInterface) {
$this->context->addViolationAt($subPath, $message, $parameters, $value);
} else {
$this->context->buildViolation($message)
->atPath($subPath)
->setParameters($parameters)
->setTranslationDomain($translationDomain)
->setInvalidValue($value)
->addViolation();
}

$this->errors[] = [$message, $parameters, $value];

Expand Down
11 changes: 10 additions & 1 deletion tests/Form/Validator/ErrorElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,16 @@ public function testAddViolation(): void
/**
* @group legacy
*/
public function testAddConstraint(): void
public function testAddViolationWithTranslationDomain()
{
$this->errorElement->addViolation(['Foo error message', ['bar_param' => 'bar_param_lvalue'], 'BAR'], [], null, 'translation_domain');
$this->assertSame([['Foo error message', ['bar_param' => 'bar_param_lvalue'], 'BAR']], $this->errorElement->getErrors());
}

/**
* @group legacy
*/
public function testAddConstraint()
{
$constraint = new NotNull();
if ($this->context instanceof LegacyExecutionContextInterface) {
Expand Down

0 comments on commit 19c7c7c

Please sign in to comment.