Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Last active November 9, 2023 10:39
Show Gist options
  • Save bwaidelich/4606056 to your computer and use it in GitHub Desktop.
Save bwaidelich/4606056 to your computer and use it in GitHub Desktop.
TYPO3 Flow – Unique & Exists Validators
<?php
namespace My\Package\Validation;
use TYPO3\Flow\Persistence\RepositoryInterface;
class ExistsValidator extends \TYPO3\Flow\Validation\Validator\AbstractValidator {
/**
* @var array
*/
protected $supportedOptions = array(
'repository' => array(NULL, 'Repository to look for the unique property', 'string', TRUE),
'propertyName' => array(NULL, 'name of the unique property', 'string', TRUE),
);
/**
* @param mixed $value The value that should be validated
* @return void
* @throws \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException
*/
protected function isValid($value) {
$repository = new $this->options['repository']();
if (!$repository instanceof RepositoryInterface) {
throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('The option "repository" must refer to a class implementing RepositoryInterface.', 1336499435);
}
$propertyName = (string)$this->options['propertyName'];
$query = $repository->createQuery();
$numberOfResults = $query->matching($query->equals($propertyName, $value))->count();
if ($numberOfResults === 0) {
$this->addError('This %s was not found', 1340810986, array($propertyName));
}
}
}
?>
<?php
namespace My\Package\Validation;
use TYPO3\Flow\Persistence\RepositoryInterface;
class UniqueValidator extends \TYPO3\Flow\Validation\Validator\AbstractValidator {
/**
* @var array
*/
protected $supportedOptions = array(
'repository' => array(NULL, 'Repository to look for the unique property', 'string', TRUE),
'propertyName' => array(NULL, 'name of the unique property', 'string', TRUE),
'alwaysAllow' => array(NULL, 'if the unique property is equal to this option, the validator is disabled', 'mixed'),
);
/**
* @param mixed $value The value that should be validated
* @return void
* @throws \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException
*/
protected function isValid($value) {
$repository = new $this->options['repository']();
if (!$repository instanceof RepositoryInterface) {
throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('The option "repository" must refer to a class implementing RepositoryInterface.', 1336499435);
}
if (isset($this->options['alwaysAllow']) && $value === $this->options['alwaysAllow']) {
return;
}
$propertyName = (string)$this->options['propertyName'];
$query = $repository->createQuery();
$numberOfResults = $query->matching($query->equals($propertyName, $value))->count();
if ($numberOfResults > 0) {
$this->addError('This %s is already taken', 1336499565, array($propertyName));
}
}
}
?>
@andre-dbs
Copy link

Good Morning,
after implementation of the UniqueValidator like that, I get the error #1336499435: The option "repository" must implement RepositoryInterface.
How do I have to implement the RepositoryInterface?

@bwaidelich
Copy link
Author

@itoop This validator was meant to be used in conjunction with the Flow Form Framework IIRC. There you can use complex types in validator options..

I've just changed the example so that it should work with class names instead (see https://gist.github.com/bwaidelich/4606056/revisions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment