Skip to content

Instantly share code, notes, and snippets.

@jpgreth
Forked from bwaidelich/ExistsValidator.php
Created July 7, 2016 13:31
Show Gist options
  • Select an option

  • Save jpgreth/ed7aa7ce81024d708e3c49872ffb3ac5 to your computer and use it in GitHub Desktop.

Select an option

Save jpgreth/ed7aa7ce81024d708e3c49872ffb3ac5 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));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment