-
-
Save vixe76/266951d45a5085d31f29 to your computer and use it in GitHub Desktop.
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; | |
| class ExistsValidator extends \TYPO3\Flow\Validation\Validator\AbstractValidator { | |
| /** | |
| * @var array | |
| */ | |
| protected $supportedOptions = array( | |
| 'repository' => array(NULL, 'Repository to look for the unique property', 'TYPO3\Flow\Persistence\RepositoryInterface', 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 = $this->options['repository']; | |
| if (!$repository instanceof \TYPO3\Flow\Persistence\RepositoryInterface) { | |
| throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('The option "repository" must implement 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; | |
| class UniqueValidator extends \TYPO3\Flow\Validation\Validator\AbstractValidator { | |
| /** | |
| * @var array | |
| */ | |
| protected $supportedOptions = array( | |
| 'repository' => array(NULL, 'Repository to look for the unique property', 'TYPO3\Flow\Persistence\RepositoryInterface', 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 = $this->options['repository']; | |
| if (!$repository instanceof \TYPO3\Flow\Persistence\RepositoryInterface) { | |
| throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('The option "repository" must implement 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