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