当前位置: 首页>>代码示例>>PHP>>正文


PHP ValidatorChain::addValidator方法代码示例

本文整理汇总了PHP中Zend\Validator\ValidatorChain::addValidator方法的典型用法代码示例。如果您正苦于以下问题:PHP ValidatorChain::addValidator方法的具体用法?PHP ValidatorChain::addValidator怎么用?PHP ValidatorChain::addValidator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Zend\Validator\ValidatorChain的用法示例。


在下文中一共展示了ValidatorChain::addValidator方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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')));
 }
开发者ID:navassouza,项目名称:zf2,代码行数:9,代码来源:ValidatorTest.php

示例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->addValidator(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;
 }
开发者ID:robertodormepoco,项目名称:zf2,代码行数:17,代码来源:AbstractProtocol.php

示例3: __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->addValidator($v);
             }
         } else {
             throw new InvalidArgumentException('Cannot use ' . gettype($validators) . ' as validators for ' . __CLASS__);
         }
     }
     $this->parts = $this->parseRouteDefinition($route);
 }
开发者ID:royaltyclubvp,项目名称:BuzzyGals,代码行数:42,代码来源:Simple.php

示例4: testAllowsPrependingValidatorsByName

 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);
 }
开发者ID:nieldm,项目名称:zf2,代码行数:7,代码来源:ValidatorChainTest.php

示例5: _validateRule

 /**
  * @param array $validatorRule
  * @return void
  */
 protected function _validateRule(array $validatorRule)
 {
     /**
      * Get one or more data values from input, and check for missing fields.
      * Apply defaults if fields are missing.
      */
     $data = array();
     foreach ((array) $validatorRule[self::FIELDS] as $key => $field) {
         if (array_key_exists($field, $this->_data)) {
             $data[$field] = $this->_data[$field];
         } else {
             if (isset($validatorRule[self::DEFAULT_VALUE])) {
                 /** @todo according to this code default value can't be an array. It has to be reviewed */
                 if (!is_array($validatorRule[self::DEFAULT_VALUE])) {
                     // Default value is a scalar
                     $data[$field] = $validatorRule[self::DEFAULT_VALUE];
                 } else {
                     // Default value is an array. Search for corresponding key
                     if (isset($validatorRule[self::DEFAULT_VALUE][$key])) {
                         $data[$field] = $validatorRule[self::DEFAULT_VALUE][$key];
                     } else {
                         if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
                             // Default value array is provided, but it doesn't have an entry for current field
                             // and presence is required
                             $this->_missingFields[$validatorRule[self::RULE]][] = $this->_getMissingMessage($validatorRule[self::RULE], $field);
                         }
                     }
                 }
             } else {
                 if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
                     $this->_missingFields[$validatorRule[self::RULE]][] = $this->_getMissingMessage($validatorRule[self::RULE], $field);
                 }
             }
         }
     }
     /**
      * If any required fields are missing, break the loop.
      */
     if (isset($this->_missingFields[$validatorRule[self::RULE]]) && count($this->_missingFields[$validatorRule[self::RULE]]) > 0) {
         return;
     }
     /**
      * Evaluate the inputs against the validator chain.
      */
     if (count((array) $validatorRule[self::FIELDS]) > 1) {
         if (!$validatorRule[self::ALLOW_EMPTY]) {
             $emptyFieldsFound = false;
             $errorsList = array();
             $messages = array();
             foreach ($data as $fieldKey => $field) {
                 $notEmptyValidator = $this->_getValidator('NotEmpty');
                 $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
                 if (!$notEmptyValidator->isValid($field)) {
                     foreach ($notEmptyValidator->getMessages() as $messageKey => $message) {
                         if (!isset($messages[$messageKey])) {
                             $messages[$messageKey] = $message;
                         } else {
                             $messages[] = $message;
                         }
                     }
                     $errorsList[] = $notEmptyValidator->getErrors();
                     $emptyFieldsFound = true;
                 }
             }
             if ($emptyFieldsFound) {
                 $this->_invalidMessages[$validatorRule[self::RULE]] = $messages;
                 $this->_invalidErrors[$validatorRule[self::RULE]] = array_unique(call_user_func_array('array_merge', $errorsList));
                 return;
             }
         }
         if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) {
             $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages();
             $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors();
             return;
         }
     } else {
         if (count($data) > 0) {
             // $data is actually a one element array
             $fieldNames = array_keys($data);
             $fieldName = reset($fieldNames);
             $field = reset($data);
             $failed = false;
             if (!is_array($field)) {
                 $field = array($field);
             }
             $notEmptyValidator = $this->_getValidator('NotEmpty');
             $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName));
             if ($validatorRule[self::ALLOW_EMPTY]) {
                 $validatorChain = $validatorRule[self::VALIDATOR_CHAIN];
             } else {
                 $validatorChain = new Validator\ValidatorChain();
                 $validatorChain->addValidator($notEmptyValidator, true);
                 $validatorChain->addValidator($validatorRule[self::VALIDATOR_CHAIN]);
             }
             foreach ($field as $value) {
                 if ($validatorRule[self::ALLOW_EMPTY] && !$notEmptyValidator->isValid($value)) {
//.........这里部分代码省略.........
开发者ID:heiglandreas,项目名称:zf2,代码行数:101,代码来源:InputFilter.php

示例6: populateValidators

 protected function populateValidators(ValidatorChain $chain, $validators)
 {
     foreach ($validators as $validator) {
         if ($validator instanceof ValidatorInterface) {
             $chain->addValidator($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 = array();
             if (isset($validator['options'])) {
                 $options = $validator['options'];
             }
             $breakChainOnFailure = false;
             if (isset($validator['break_chain_on_failure'])) {
                 $breakChainOnFailure = $validator['break_chain_on_failure'];
             }
             $chain->addByName($name, $options, $breakChainOnFailure);
             continue;
         }
         throw new Exception\RuntimeException('Invalid validator specification provided; was neither a validator instance nor an array specification');
     }
 }
开发者ID:Rovak,项目名称:zf2,代码行数:26,代码来源:Factory.php


注:本文中的Zend\Validator\ValidatorChain::addValidator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。