/** * {@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; }
public function getMessages() { $parent = parent::getMessages(); $language = $this->languageValidator->getMessages(); $chain = $this->chain->getMessages(); return array_merge($parent, $language, $chain); }
/** * @param array $config * @param ServiceLocatorInterface $serviceLocator * @return ValidatorChain */ private function create(array $config, ServiceLocatorInterface $serviceLocator) { $validator = new ValidatorChain(); foreach ($config as $key => $val) { $breakChainOnFailure = false; if ($key === 'required') { continue; } if (is_string($key) && class_exists($key)) { if (isset($val['break']) || is_int($key)) { $breakChainOnFailure = $val['break']; } $childValidator = $this->service($key, $serviceLocator); } elseif (is_array($val)) { $childValidator = $this->create($val, $serviceLocator); if (!isset($val['required']) || $val['required'] !== false) { $childValidator->attach(new FieldExists($key), true, 2); } } else { $childValidator = $this->service($val, $serviceLocator); } $validator->attach($childValidator, $breakChainOnFailure); } return $validator; }
/** * {@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; }
/** * {@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; }
/** * @return ValidatorChain */ protected function getTitleValidatorChain() { $stringLength = new StringLength(); $stringLength->setMin(5); $validatorChain = new ValidatorChain(); // $validatorChain->attach(new Alnum(true)); $validatorChain->attach($stringLength); return $validatorChain; }
public function testValidatorChain() { $validatorChain = new ValidatorChain(); $validatorChain->addValidator(new DigitsFilter()); $validatorChain->addValidator(new Int()); $filter = new Validator($validatorChain); $this->assertTrue($filter->filter(array('message' => '123'))); $this->assertFalse($filter->filter(array('message' => 'test'))); }
/** * {@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; }
/** * Constructor. * * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1) * @param integer $port OPTIONAL Port number (default: null) * @throws Exception\RuntimeException */ public function __construct($host = '127.0.0.1', $port = null) { $this->validHost = new Validator\ValidatorChain(); $this->validHost->attach(new Validator\Hostname(Validator\Hostname::ALLOW_ALL)); if (!$this->validHost->isValid($host)) { throw new Exception\RuntimeException(implode(', ', $this->validHost->getMessages())); } $this->host = $host; $this->port = $port; }
public function createInputFilter() { $inputFilter = new InputFilter\InputFilter(); $email = new InputFilter\Input('email'); $email->setRequired(true); $validatorChain = new Validator\ValidatorChain(); $validatorChain->attach(new Validator\EmailAddress()); $email->setValidatorChain($validatorChain); $inputFilter->add($email); return $inputFilter; }
/** * {@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} */ 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; }
public function getValidatorChain() { if (!$this->validatorChain) { $this->validatorChain = new ValidatorChain(); $uploadFileValidator = new UploadFileValidator(); $this->validatorChain->attach($uploadFileValidator); $mimeTypeValidator = new MimeTypeValidator(); $mimeTypeValidator->addMimeType($this->allowedMimeTypes); $this->validatorChain->attach($mimeTypeValidator); } return $this->validatorChain; }
public function isValid($value, array $context = []) { if (!isset($context['name_of_other_field'])) { throw new Exception\RuntimeException(sprintf('The required \'name_of_other_field\' context value was not found in validator \'%s\'.', __CLASS__)); } if (1234 === $context['name_of_other_field']) { $validator = new Validator\ValidatorChain(); $validator->attach(new Validator\StringLength(['min' => 8, 'max' => 12])); $validator->attach(new Validator\EmailAddress()); return $validator->isValid($value); } return true; }
public function testAllowsPrependingValidatorsByName() { $this->validator->addValidator($this->getValidatorTrue())->prependByName('NotEmpty', array(), true); $this->assertFalse($this->validator->isValid('')); $messages = $this->validator->getMessages(); $this->assertArrayHasKey('isEmpty', $messages); }
/** * @group ZF-412 */ public function testCanAttachMultipleValidatorsOfTheSameTypeAsDiscreteInstances() { $this->validator->addByName('Callback', array('callback' => function ($value) { return true; }, 'messages' => array('callbackValue' => 'This should not be seen in the messages'))); $this->validator->addByName('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); }
/** * Create a new simple console route. * * @param string $route * @param array $constraints * @param array $defaults * @param array $aliases * @param null|array|Traversable|FilterChain $filters * @param null|array|Traversable|ValidatorChain $validators * @throws \Zend\Mvc\Exception\InvalidArgumentException * @return \Zend\Mvc\Router\Console\Simple */ public function __construct($route, array $constraints = array(), array $defaults = array(), array $aliases = array(), $filters = null, $validators = null) { $this->defaults = $defaults; $this->constraints = $constraints; $this->aliases = $aliases; if ($filters !== null) { if ($filters instanceof FilterChain) { $this->filters = $filters; } elseif ($filters instanceof Traversable) { $this->filters = new FilterChain(array('filters' => ArrayUtils::iteratorToArray($filters, false))); } elseif (is_array($filters)) { $this->filters = new FilterChain(array('filters' => $filters)); } else { throw new InvalidArgumentException('Cannot use ' . gettype($filters) . ' as filters for ' . __CLASS__); } } if ($validators !== null) { if ($validators instanceof ValidatorChain) { $this->validators = $validators; } elseif ($validators instanceof Traversable || is_array($validators)) { $this->validators = new ValidatorChain(); foreach ($validators as $v) { $this->validators->attach($v); } } else { throw new InvalidArgumentException('Cannot use ' . gettype($validators) . ' as validators for ' . __CLASS__); } } $this->parts = $this->parseRouteDefinition($route); }
public function createInputFilter() { $inputFilter = new InputFilter\InputFilter(); //username $username = new InputFilter\Input('email'); $username->setRequired(true); $validatorChain = new Validator\ValidatorChain(); $validatorChain->attach(new Validator\EmailAddress()); $username->setValidatorChain($validatorChain); $inputFilter->add($username); //password $password = new InputFilter\Input('password'); $password->setRequired(true); $inputFilter->add($password); return $inputFilter; }
protected function createInputFilter() { $inputFilter = new InputFilter\InputFilter(); // password $password = new InputFilter\Input('password'); $password->setRequired(true); // Generate password validator chain $validatorPasswordChain = new Validator\ValidatorChain(); $validatorPasswordChain->attach(new Validator\StringLength(array('min' => 6))); $password->setValidatorChain($validatorPasswordChain); $inputFilter->add($password); // confirmation $confirmation = new InputFilter\Input('confirmation'); $confirmation->setRequired(true); $confirmation->setValidatorChain($validatorPasswordChain); $inputFilter->add($confirmation); return $inputFilter; }
/** * Get current input filter factory * * If none provided, uses an unconfigured instance. * * @return InputFilterFactory */ public function getInputFilterFactory() { $inputFilterFactory = parent::getInputFilterFactory(); if (!$this->inputFilterFactoryDefaultsInitialized) { $this->inputFilterFactoryDefaultsInitialized = true; $dfc = $inputFilterFactory->getDefaultFilterChain(); $dvc = $inputFilterFactory->getDefaultValidatorChain(); if (empty($dfc)) { $inputFilterFactory->setDefaultFilterChain($dfc = new FilterChain()); } if (empty($dvc)) { $inputFilterFactory->setDefaultValidatorChain($dvc = new ValidatorChain()); } $initializer = array($this, 'initializeApplicationServiceLocators'); $dfc->getPluginManager()->addInitializer($initializer, false); $dvc->getPluginManager()->addInitializer($initializer, false); } return $inputFilterFactory; }
/** * Constructor. * * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1) * @param integer $port OPTIONAL Port number (default: null) * @throws \Zend\Mail\Protocol\Exception * @return void */ public function __construct($host = '127.0.0.1', $port = null) { $this->_validHost = new Validator\ValidatorChain(); $this->_validHost->addValidator(new HostnameValidator(HostnameValidator::ALLOW_ALL)); if (!$this->_validHost->isValid($host)) { throw new Protocol\Exception\RuntimeException(implode(', ', $this->_validHost->getMessages())); } $this->_host = $host; $this->_port = $port; }
/** * Constructor. * * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1) * @param integer $port OPTIONAL Port number (default: null) * @throws \Zend\Mail\Protocol\Exception * @return void */ public function __construct($host = '127.0.0.1', $port = null) { $this->_validHost = new Validator\ValidatorChain(); $this->_validHost->addValidator(new HostnameValidator\Hostname(HostnameValidator\Hostname::ALLOW_ALL)); if (!$this->_validHost->isValid($host)) { throw new Exception(join(', ', $this->_validHost->getMessages())); } $this->_host = $host; $this->_port = $port; }
/** * {@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; }
public function testFactoryInjectsComposedFilterAndValidatorChainsIntoInputObjectsWhenCreatingNewInputFilterObjects() { $factory = new Factory(); $filterPlugins = new Filter\FilterPluginManager(); $validatorPlugins = new Validator\ValidatorPluginManager(); $filterChain = new Filter\FilterChain(); $validatorChain = new Validator\ValidatorChain(); $filterChain->setPluginManager($filterPlugins); $validatorChain->setPluginManager($validatorPlugins); $factory->setDefaultFilterChain($filterChain); $factory->setDefaultValidatorChain($validatorChain); $inputFilter = $factory->createInputFilter(array('foo' => array('name' => 'foo'))); $this->assertInstanceOf('Zend\\InputFilter\\InputFilterInterface', $inputFilter); $this->assertEquals(1, count($inputFilter)); $input = $inputFilter->get('foo'); $this->assertInstanceOf('Zend\\InputFilter\\InputInterface', $input); $inputFilterChain = $input->getFilterChain(); $inputValidatorChain = $input->getValidatorChain(); $this->assertSame($filterPlugins, $inputFilterChain->getPluginManager()); $this->assertSame($validatorPlugins, $inputValidatorChain->getPluginManager()); }
/** * @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']); }
protected function createInputFilter() { $inputFilter = new InputFilter\InputFilter(); //type $type = new InputFilter\Input('type'); $type->setRequired(true); $inputFilter->add($type); //subject $subject = new InputFilter\Input('subject'); $subject->setRequired(true); $inputFilter->add($subject); //content $content = new InputFilter\Input('content'); $content->setRequired(true); $inputFilter->add($content); //language $language = new InputFilter\Input('content'); $language->setRequired(true); $validatorChain = new Validator\ValidatorChain(); $validatorChain->attach(new Validator\Between(array('min' => 0, 'max' => 1))); $language->setValidatorChain($validatorChain); $inputFilter->add($language); return $inputFilter; }
/** * {@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 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; }
/** * @param array $value * */ protected function __construct(array $value) { /** @var \AGmakonts\STL\String\String $name */ $name = $value[0]; $nameValue = $name->value(); $validatorChain = new ValidatorChain(); $validatorChain->attach(new AlphaValidator(TRUE)); $validatorChain->attach(new StringLength(['min' => self::MINIMUM_LENGTH])); $validatorChain->attach(new NotEmpty()); if (FALSE === $validatorChain->isValid($nameValue)) { throw new InvalidNameException($name, $validatorChain->getMessages()); } $this->name = $name; }
/** * Ensures that a validator with constructor arguments can be called * with the static method is(). */ public function testStaticFactoryWithConstructorArguments() { $this->markTestSkipped('is() method should not try to implement its own plugin loader - refactor this'); $this->assertTrue(Validator\ValidatorChain::execute('12', 'Between', array('min' => 1, 'max' => 12))); $this->assertFalse(Validator\ValidatorChain::execute('24', 'Between', array('min' => 1, 'max' => 12))); }