/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $validatorChain = new ValidatorChain(); $intl = new IntlDateFormatter(Locale::getDefault(), IntlDateFormatter::LONG, IntlDateFormatter::NONE); $date = (new DateTime('now'))->modify('-100 years'); $validatorChain->attachByName('GreaterThan', ['messages' => [GreaterThan::NOT_GREATER_INCLUSIVE => 'The date of birth ' . 'must be not earlier than %min% inclusive'], 'messageVariables' => ['min' => ['abstractOptions' => 'fmt']], 'min' => $date->format('Y-m-d'), 'fmt' => $intl->format($date), 'inclusive' => true], true); $date = (new DateTime('now'))->modify('-18 years'); $validatorChain->attachByName('LessThan', ['messages' => [LessThan::NOT_LESS_INCLUSIVE => 'The date of birth ' . 'must be not later than %max% inclusive'], 'messageVariables' => ['max' => ['abstractOptions' => 'fmt']], 'max' => $date->format('Y-m-d'), 'fmt' => $intl->format($date), 'inclusive' => true], true); return $validatorChain; }
/** * Validator configuration for the values of localization * @param string[] $validators */ public function setValidators($validators) { $this->chain = new \Zend\Validator\ValidatorChain(); foreach ($validators as $validator) { if (!isset($validator['options'])) { $validator['options'] = []; } $this->chain->attachByName($validator['name'], $validator['options']); } }
/** * {@inheritDoc} * * @return ValidatorChain */ public function createService(ServiceLocatorInterface $validators) { /* @var $options InputFilterOptionsInterface */ $options = $validators->getServiceLocator()->get(ModuleOptions::class); $chain = new ValidatorChain(); $chain->attachByName('StringLength', ['min' => $options->getMinIdentityLength(), 'max' => $options->getMaxIdentityLength()], true); if ($options->getIdentityRegexPattern()) { $chain->attachByName('Regex', ['messages' => [Regex::NOT_MATCH => 'Incorrect identity. ' . 'Identity must contain alphanumeric characters without spaces'], 'pattern' => $options->getIdentityRegexPattern()], true); } return $chain; }
/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options InputFilterOptionsInterface */ $options = $services->get(ModuleOptions::class); $validator = new ValidatorChain(); $validator->setPluginManager($serviceLocator); $validator->attachByName('StringLength', ['messages' => [StringLength::TOO_SHORT => 'Email address must be at least %min% characters long', StringLength::TOO_LONG => 'Email address must not be more than %max% characters'], 'encoding' => 'UTF-8', 'min' => 6, 'max' => 60], true); $validator->attachByName('EmailAddress', [], true); $validator->attachByName('CmsUserNoEmailExists', [], true); return $validator; }
/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options InputFilterOptionsInterface */ $options = $services->get(ModuleOptions::class); $validator = new ValidatorChain(); $validator->setPluginManager($serviceLocator); $validator->attachByName('StringLength', ['messages' => [StringLength::TOO_SHORT => 'The username must be at least %min% characters long', StringLength::TOO_LONG => 'The username must not be more than %max% characters'], 'encoding' => 'UTF-8', 'min' => 5, 'max' => 30], true); $validator->attachByName('Regex', ['messages' => [Regex::NOT_MATCH => 'Incorrect username. ' . 'Username must contain alphanumeric characters without spaces'], 'pattern' => $options->getUsernameRegexPattern()], true); $validator->attachByName('CmsUserNoUsernameExists', [], true); return $validator; }
/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options ModuleOptions */ $options = $services->get(ModuleOptions::class); $identity = null; if ($services->has($options->getAuthenticationService())) { /* @var $authService \Zend\Authentication\AuthenticationServiceInterface */ $authService = $services->get($options->getAuthenticationService()); if ($authService->hasIdentity()) { /* @var $identity \CmsUser\Mapping\UserInterface */ $identity = $authService->getIdentity(); } } $validatorChain = new ValidatorChain(); $validatorChain->attachByName('Callback', ['messages' => [Callback::INVALID_VALUE => 'Your answer is wrong. ' . 'Please provide the correct answer'], 'callback' => function ($value, $context = []) use($identity) { if (isset($context['answer'])) { return strtolower($context['answer']) === $value; } elseif ($identity) { return $identity->getAnswer() === $value; } return false; }], true); return $validatorChain; }
/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options InputFilterOptionsInterface */ $options = $services->get(ModuleOptions::class); $userMapper = null; $identity = null; if ($services->has($options->getAuthenticationService())) { /* @var $authService \Zend\Authentication\AuthenticationServiceInterface */ $authService = $services->get($options->getAuthenticationService()); if ($authService->hasIdentity()) { /* @var $userMapper \CmsUser\Persistence\UserMapperInterface */ $userMapper = $services->get('MapperManager')->get($options->getUserEntityClass()); /* @var $identity \CmsUser\Mapping\UserInterface */ $identity = $authService->getIdentity(); } } $validatorChain = new ValidatorChain(); $validatorChain->attachByName('Callback', ['messages' => [Callback::INVALID_VALUE => 'Incorrect password verification'], 'callback' => function ($value, $context = []) use($userMapper, $identity) { if (isset($context['password'])) { return $value === $context['password']; } elseif ($userMapper && $identity && $identity instanceof PasswordableInterface) { return $userMapper->getPasswordService()->verify($value, $identity->getPassword()); } return false; }], true); return $validatorChain; }
/** * @group ZF-412 */ public function testCanAttachMultipleValidatorsOfTheSameTypeAsDiscreteInstances() { $this->validator->attachByName('Callback', array('callback' => function ($value) { return true; }, 'messages' => array('callbackValue' => 'This should not be seen in the messages'))); $this->validator->attachByName('Callback', array('callback' => function ($value) { return false; }, 'messages' => array('callbackValue' => 'Second callback trapped'))); $this->assertEquals(2, count($this->validator)); $validators = $this->validator->getValidators(); $compare = null; foreach ($validators as $validator) { $this->assertNotSame($compare, $validator); $compare = $validator; } $this->assertFalse($this->validator->isValid('foo')); $messages = $this->validator->getMessages(); $found = false; $test = 'Second callback trapped'; foreach ($messages as $messageSet) { if (is_string($messageSet) && $messageSet === $test) { $found = true; break; } if (is_array($messageSet) && in_array('Second callback trapped', $messageSet)) { $found = true; break; } } $this->assertTrue($found); }
/** * {@inheritDoc} * * @return ValidatorChain */ public function createService(ServiceLocatorInterface $validators) { /* @var $options InputFilterOptionsInterface */ $options = $validators->getServiceLocator()->get(ModuleOptions::class); $chain = new ValidatorChain(); $chain->attachByName('StringLength', ['min' => $options->getMinCredentialLength(), 'max' => $options->getMaxCredentialLength()], true); return $chain; }
/** * {@inheritDoc} */ public function createService(ServiceLocatorInterface $validators) { $validatorChain = new ValidatorChain(); $validatorChain->attachByName('Callback', ['messages' => [Callback::INVALID_VALUE => 'Termination date must be greater' . ' than the date of employment'], 'callback' => function ($value, $context = []) { if ($value && isset($context['since'])) { $filter = new DateSelectFilter(); return new \DateTime($value) > new \DateTime($filter->filter($context['since'])); } return true; }], true); return $validatorChain; }
/** * @group zfcampus_zf-apigility-admin_89 * @dataProvider breakChainFlags */ public function testAttachByNameAllowsSpecifyingBreakChainOnFailureFlagViaOptions($option) { $this->validator->attachByName('GreaterThan', array($option => true, 'min' => 1)); $this->assertEquals(1, count($this->validator)); $validators = $this->validator->getValidators(); $spec = array_shift($validators); $this->assertInternalType('array', $spec); $this->assertArrayHasKey('instance', $spec); $validator = $spec['instance']; $this->assertInstanceOf('Zend\\Validator\\GreaterThan', $validator); $this->assertArrayHasKey('breakChainOnFailure', $spec); $this->assertTrue($spec['breakChainOnFailure']); }
/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options InputFilterOptionsInterface */ $options = $services->get(ModuleOptions::class); /* @var $userMapper \CmsUser\Persistence\UserMapperInterface */ $userMapper = $services->get('MapperManager')->get($options->getUserEntityClass()); $identityField = $services->get('FormElementManager')->get('CmsAuthenticationIdentity')->getName(); $validatorChain = new ValidatorChain(); $validatorChain->attachByName('Callback', ['messages' => [Callback::INVALID_VALUE => 'Your birthday is wrong. ' . 'Please provide the correct birthday'], 'callback' => function ($value, $context = []) use($userMapper, $identityField) { if (!empty($context[$identityField])) { if ($identity = $userMapper->findByIdentity($context[$identityField])) { return new \DateTime($value) == $identity->getBirthday(); } } return true; }], true); return $validatorChain; }
/** * @param ValidatorChain $chain * @param array|Traversable $validators * @throws Exception\RuntimeException * @return void */ protected function populateValidators(ValidatorChain $chain, $validators) { foreach ($validators as $validator) { if ($validator instanceof ValidatorInterface) { $chain->attach($validator); continue; } if (is_array($validator)) { if (!isset($validator['name'])) { throw new Exception\RuntimeException('Invalid validator specification provided; does not include "name" key'); } $name = $validator['name']; $options = []; if (isset($validator['options'])) { $options = $validator['options']; } $breakChainOnFailure = false; if (isset($validator['break_chain_on_failure'])) { $breakChainOnFailure = $validator['break_chain_on_failure']; } $chain->attachByName($name, $options, $breakChainOnFailure); continue; } throw new Exception\RuntimeException('Invalid validator specification provided; was neither a validator instance nor an array specification'); } }