本文整理汇总了PHP中Zend\Validator\ValidatorChain类的典型用法代码示例。如果您正苦于以下问题:PHP ValidatorChain类的具体用法?PHP ValidatorChain怎么用?PHP ValidatorChain使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ValidatorChain类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createService
/**
* {@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;
}
示例2: create
/**
* @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;
}
示例3: createService
/**
* {@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;
}
示例4: createService
/**
* {@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;
}
示例5: getTitleValidatorChain
/**
* @return ValidatorChain
*/
protected function getTitleValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(5);
$validatorChain = new ValidatorChain();
// $validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
示例6: testValidatorChain
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')));
}
示例7: createService
/**
* {@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;
}
示例8: __construct
/**
* 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;
}
示例9: createInputFilter
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;
}
示例10: createService
/**
* {@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;
}
示例11: createService
/**
* {@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;
}
示例12: getValidatorChain
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;
}
示例13: isValid
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;
}
示例14: __construct
/**
* 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);
}
示例15: createInputFilter
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;
}