Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Initialize validator messages before validation
Browse files Browse the repository at this point in the history
This allows more flexibility for the messages initialization; for
instance you can dynamically build the messages list in the method
`initializeObject()` of your validator, then they will be processed just
before the validation actually runs.
  • Loading branch information
romm committed Aug 11, 2017
1 parent 2c4763f commit 0be97eb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Classes/Validation/Validator/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Romm\Formz\Form\FormInterface;
use Romm\Formz\Service\MessageService;
use Romm\Formz\Validation\DataObject\ValidatorDataObject;
use TYPO3\CMS\Extbase\Error\Result;

abstract class AbstractValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
{
Expand Down Expand Up @@ -96,11 +97,31 @@ final public function __construct(array $options = [], ValidatorDataObject $data

$this->dataObject = $dataObject;
$this->form = $dataObject->getFormObject()->getForm();
}

/**
* @param mixed $value
* @return Result
*/
public function validate($value)
{
/*
* Messages are initialized just before the validation actually runs,
* and not in the constructor.
*
* This allows more flexibility for the messages initialization; for
* instance you can dynamically build the messages list in the method
* `initializeObject()` of your validator (and not only in the class
* variable declaration), then the messages will be processed just
* below.
*/
$this->messages = MessageService::get()->filterMessages(
$this->dataObject->getValidation()->getMessages(),
$this->supportedMessages,
(bool)$this->supportsAllMessages
);

return parent::validate($value);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions Tests/Fixture/Validation/Validator/DummyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class DummyValidator extends AbstractValidator
{
const MESSAGE_1 = 'message1';
const DYNAMIC_MESSAGE = 'dynamic_message';

/**
* @var array
Expand All @@ -26,6 +27,16 @@ class DummyValidator extends AbstractValidator
*/
protected $callback;

/**
* Dynamically assigns a message to the list of supported messages.
*
* @see \Romm\Formz\Tests\Unit\Validation\Validator\AbstractValidatorTest::runValidatorDataProvider #9
*/
public function initializeObject()
{
$this->supportedMessages[self::DYNAMIC_MESSAGE]['value'] = 'dynamic message';
}

/**
* @param mixed $value
*/
Expand Down
23 changes: 23 additions & 0 deletions Tests/Unit/Validation/Validator/AbstractValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function runValidator($value, array $messages, $callback, $finalCallback,
$validatorDataObject = new ValidatorDataObject($this->getDefaultFormObject(), $validation);

$validator = new DummyValidator([], $validatorDataObject);
$validator->initializeObject();

if (is_callable($callback)) {
$validator->setCallBack($callback);
}
Expand Down Expand Up @@ -205,6 +207,27 @@ public function runValidatorDataProvider()
$this->assertEquals(42, $message->getCode());
$this->assertEquals('baz', $message->getTitle());
}
],
/*
* #9
*
* Messages can be assigned dynamically (outside of the variable
* declaration scope). The code below will test that a dynamic
* message can be retrieved after the validation process.
*/
[
'value' => 'foo',
'messages' => [],
'callback' => function (DummyValidator $validator) {
$validator->addNewError(DummyValidator::DYNAMIC_MESSAGE, 42, [], 'foo');
},
'final' => function (Result $result) {
$this->assertTrue($result->hasErrors());
$message = $result->getFirstError();
$this->assertEquals('dynamic message', $message->getMessage());
$this->assertEquals(42, $message->getCode());
$this->assertEquals('foo', $message->getTitle());
}
]
];
}
Expand Down

0 comments on commit 0be97eb

Please sign in to comment.