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'))); }
/** * 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 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; }
/** * @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; }
/** * @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; }
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); }
/** * 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); }
/** * @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 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; }
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; }
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; }
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; }
/** * 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']; } else { throw new \Exception('Não foi definido nenhuma coluna de exclusão!', 500); } if (isset($params['excludeColValue']) and !empty($params['excludeColValue'])) { $options['exclude']['value'] = \Cityware\Format\Str::preparePhpTag($params['excludeColValue'], false); } else { if (isset($params['excludeUrlParam']) and !empty($params['excludeUrlParam'])) { $options['exclude']['value'] = $this->getUrlParam($params['excludeCol']); } else { throw new \Exception('Não foi definido nenhum valor de exclusão!', 500); } } } $validatorsField['validators'][$iCount] = array('name' => 'Db\\RecordExists', 'options' => $options); break; case 'norecordexists': $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']; } else { throw new \Exception('Não foi definido nenhuma coluna de exclusão!', 500); } if (isset($params['excludeColValue']) and !empty($params['excludeColValue'])) { $options['exclude']['value'] = \Cityware\Format\Str::preparePhpTag($params['excludeColValue'], false); } else { if (isset($params['excludeUrlParam']) and !empty($params['excludeUrlParam'])) { $options['exclude']['value'] = $this->getUrlParam($params['excludeCol']); } else { throw new \Exception('Não foi definido nenhum valor de exclusão!', 500); } } } $validatorsField['validators'][$iCount] = array('name' => 'Db\\NoRecordExists', 'options' => $options); break; case 'digits': case 'int': case 'integer': $validatorsField['validators'][$iCount] = array('name' => 'Digits', 'options' => $options); break; case 'email': $validatorsField['validators'][$iCount] = array('name' => 'EmailAddress', 'options' => array($options, 'allow' => \Zend\Validator\Hostname::ALLOW_DNS, 'mx' => false, 'domain' => true)); break; case 'greaterthan': if (isset($params['validationmin']) and !empty($params['validationmin'])) { $options['min'] = $params['validationmin']; } $validatorsField['validators'][$iCount] = array('name' => 'GreaterThan', 'options' => $options); break; case 'hex': $validatorsField['validators'][$iCount] = array('name' => 'Hex', 'options' => $options); break; case 'hostname': $validatorsField['validators'][$iCount] = array('name' => 'Hostname', 'options' => $options); break; case 'iban': $validatorsField['validators'][$iCount] = array('name' => 'Iban', 'options' => $options); break; case 'identical': $validatorsField['validators'][$iCount] = array('name' => 'Identical', 'options' => array($options, 'token' => $params['validationcompare'])); break; case 'ip': $validatorsField['validators'][$iCount] = array('name' => 'Ip', 'options' => $options); break; case 'isbn': $validatorsField['validators'][$iCount] = array('name' => 'Isbn', 'options' => $options); break; case 'postcode': $validatorsField['validators'][$iCount] = array('name' => 'PostCode', 'options' => $options); break; case 'lessthan': if (isset($params['validationmax']) and !empty($params['validationmax'])) { $options['max'] = $params['validationmax']; } $validatorsField['validators'][$iCount] = array('name' => 'LessThan', 'options' => $options); break; case 'regex': $validatorsField['validators'][$iCount] = array('name' => 'Regex', 'options' => array($options, 'pattern' => $params['regexrule'])); break; case 'stringlength': if (isset($params['validationmax']) and !empty($params['validationmax'])) { $options['max'] = $params['validationmax']; } if (isset($params['validationmin']) and !empty($params['validationmin'])) { $options['min'] = $params['validationmin']; } $validatorsField['validators'][$iCount] = array('name' => 'StringLength', 'options' => $options); break; /* Customizados */ /* Customizados */ case 'custom': $validatorsField['validators'][$iCount] = $validatorChain->attach(new $params['customclass']()); break; case 'cpf': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Cpf()); break; case 'cnpj': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Cnpj()); break; case 'renavam': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Renavam()); break; case 'rg': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\Rg()); break; case 'strongpassword': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\StrongPassword()); break; case 'mediumpassword': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\MediumPassword()); break; case 'easypassword': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\EasyPassword()); break; case 'float': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Zend\I18n\Validator\Float()); break; case 'seterrorcustom': $validatorsField['validators'][$iCount] = $validatorChain->attach(new \Cityware\Form\Validators\SetErrorCustom()); break; } if (isset($params['messages']) and !empty($params['messages'])) { $validatorsField['validators'][$iCount]['options']['messages'] = $params['messages']; } $iCount++; } } $mainFilter->add($factory->createInput($validatorsField)); return $mainFilter; }
/** * @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'); } }
/** * @param array $data * * @return bool */ private function areTokensValid(array $data) { $validatorChain = new ValidatorChain(); $validatorChain->attach(new Regex('/^\\{[A-Z]+\\}$/')); foreach ($data as $token => $value) { if (FALSE === $validatorChain->isValid($token)) { return FALSE; } } return TRUE; }