Last active
November 9, 2023 10:39
-
-
Save bwaidelich/4606056 to your computer and use it in GitHub Desktop.
TYPO3 Flow – Unique & Exists Validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
} | |
} | |
?> |
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?
@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
Cool one!