本文整理汇总了PHP中Zend\Validator\ValidatorChain::attach方法的典型用法代码示例。如果您正苦于以下问题:PHP ValidatorChain::attach方法的具体用法?PHP ValidatorChain::attach怎么用?PHP ValidatorChain::attach使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Validator\ValidatorChain
的用法示例。
在下文中一共展示了ValidatorChain::attach方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testValidatorChain
public function testValidatorChain()
{
$validatorChain = new ValidatorChain();
$validatorChain->attach(new DigitsFilter());
$validatorChain->attach(new Int());
$filter = new Validator($validatorChain);
$this->assertTrue($filter->filter(array('message' => '123')));
$this->assertFalse($filter->filter(array('message' => 'test')));
}
示例2: __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;
}
示例3: 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;
}
示例4: 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;
}
示例5: __construct
/**
* @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;
}
示例6: 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;
}
示例7: testAllowsPrependingValidatorsByName
public function testAllowsPrependingValidatorsByName()
{
$this->validator->attach($this->getValidatorTrue())->prependByName('NotEmpty', array(), true);
$this->assertFalse($this->validator->isValid(''));
$messages = $this->validator->getMessages();
$this->assertArrayHasKey('isEmpty', $messages);
}
示例8: __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);
}
示例9: 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;
}
示例10: 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;
}
示例11: 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;
}
示例12: createInputFilter
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;
}
示例13: createInputFilter
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;
}
示例14: getInputFilter
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array('name' => 'userID', 'required' => true, 'filters' => array(array('name' => 'Int')))));
//用户名
$usernameif = $factory->createInput(array('name' => 'username', 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim'))));
$validatorChain = new ValidatorChain();
$v = new StringLength(array('min' => 6, 'max' => 20, 'encoding' => 'UTF-8'));
$v->setMessages(array(StringLength::TOO_SHORT => '用户名 \'%value%\' 太短了,至少需要6个字符', StringLength::TOO_LONG => '用户名 \'%value%\' 太长了,最多20个字符'));
$validatorChain->attach($v);
$v = new NotEmpty();
$v->setMessage(array(NotEmpty::IS_EMPTY => '用户名不能为空'));
$validatorChain->attach($v);
$usernameif->setValidatorChain($validatorChain);
$inputFilter->add($usernameif);
//密码
$psif = $factory->createInput(array('name' => 'upassword', 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim'))));
$validatorChain = new ValidatorChain();
$v = new StringLength(array('min' => 6, 'max' => 20, 'encoding' => 'UTF-8'));
$v->setMessages(array(StringLength::TOO_SHORT => '密码\'%value%\' 太短了,至少需要6个字符', StringLength::TOO_LONG => '密码 \'%value%\' 太长了,最多20个字符'));
$validatorChain->attach($v);
$v = new NotEmpty();
$v->setMessage(array(NotEmpty::IS_EMPTY => '密码不能为空'));
$validatorChain->attach($v);
$psif->setValidatorChain($validatorChain);
$inputFilter->add($psif);
/* 原始方法不能改变默认错误信息
$inputFilter->add($factory->createInput(array(
'name' => 'upassword',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 20,
),
),
),
)));
*/
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
示例15: defineValidation
/**
* Função de definição de validações de campos
* @param array $params
* @param object $mainFilter
* @return object
*/
private function defineValidation(array $params, $mainFilter)
{
$factory = new ZendInputFilterFactory();
$validatorChain = new ZendValidatorChain();
$validatorsField = array();
$iCount = 0;
if (isset($params['validation']) and strtolower(trim($params['validation'])) == 'required') {
$validatorsField['name'] = $params['name'];
$validatorsField['required'] = true;
$validatorsField['validators'][$iCount] = array('name' => 'NotEmpty');
$validatorsField['validators'][$iCount]['options']['messages'] = array(\Zend\Validator\NotEmpty::IS_EMPTY => 'Campo de preenchimento obrigatório');
$iCount++;
} else {
$validatorsField['name'] = $params['name'];
$validatorsField['required'] = false;
}
if (isset($params['validationtype'])) {
/* Verifica o tipo de validação utilizada e formata para continuar */
if (!is_array($params['validationtype'])) {
$params['validationtype'] = array($params['validationtype']);
}
/* Procura o tipo de validação e define o mesmo */
foreach ($params['validationtype'] as $value) {
$options = array();
//$options['translator'] = $this->translator;
switch (strtolower($value)) {
/* Zend Validator */
case 'alphanum':
if (isset($params['permiteespaco']) and !empty($params['permiteespaco'])) {
$validatorsField['validators'][$iCount] = array('name' => 'Alnum', 'options' => array($options, 'allowWhiteSpace' => true));
} else {
$validatorsField['validators'][$iCount] = array('name' => 'Alnum');
}
break;
case 'alpha':
if (isset($params['permiteespaco']) and !empty($params['permiteespaco'])) {
$validatorsField['validators'][$iCount] = array('name' => 'Alpha', 'options' => array($options, 'allowWhiteSpace' => true));
} else {
$validatorsField['validators'][$iCount] = array('name' => 'Alpha');
}
break;
case 'barcode':
$validatorsField['validators'][$iCount] = array('name' => 'Barcode', 'options' => array($options, 'adapter' => $params['barcodeadapter']));
break;
case 'between':
if (isset($params['validationmax']) and !empty($params['validationmax'])) {
$options['max'] = $params['validationmax'];
}
if (isset($params['validationmin']) and !empty($params['validationmin'])) {
$options['min'] = $params['validationmin'];
}
$options['inclusive'] = true;
$validatorsField['validators'][$iCount] = array('name' => 'Between', 'options' => $options);
break;
case 'callback':
$validatorsField['validators'][$iCount] = array('name' => 'Callback', 'options' => $options);
break;
case 'creditcard':
$validatorsField['validators'][$iCount] = array('name' => 'CreditCard', 'options' => $options);
break;
case 'date':
$options['format'] = 'Y-m-d';
$validatorsField['validators'][$iCount] = array('name' => 'Date', 'options' => $options);
break;
case 'time':
$options['format'] = 'H:i:s';
$validatorsField['validators'][$iCount] = array('name' => 'Date', 'options' => $options);
break;
case 'datetime':
$validatorObj = new \Zend\I18n\Validator\DateTime();
$validatorObj->setDateType(\IntlDateFormatter::SHORT);
$validatorObj->setTimeType(\IntlDateFormatter::SHORT);
$validatorsField['validators'][$iCount] = $validatorChain->attach($validatorObj);
break;
case 'recordexists':
$options['adapter'] = \Cityware\Db\Factory::factory('zend')->getAdapter();
if (isset($params['recordColumn']) and !empty($params['recordColumn'])) {
$options['field'] = $params['recordColumn'];
} else {
$options['field'] = $params['name'];
}
if (isset($params['recordTable']) and !empty($params['recordTable'])) {
$options['table'] = $params['recordTable'];
} else {
$options['table'] = $this->formDefaultConfig['table'];
}
if (isset($params['recordSchema']) and !empty($params['recordSchema'])) {
$options['schema'] = $params['recordSchema'];
} else {
$options['schema'] = $this->formDefaultConfig['schema'];
}
if (isset($params['exclude']) and $params['exclude'] == 'true') {
if (isset($params['excludeCol']) and !empty($params['excludeCol'])) {
$options['exclude']['field'] = $params['excludeCol'];
//.........这里部分代码省略.........