/** * @inheritdoc */ protected function attachDefaultValidators(InputInterface $input, DOMElement $element) { $min = $element->getAttribute('min'); $max = $element->getAttribute('max'); if ($min && $max) { $input->getValidatorChain()->attach(new Validator\Between(['min' => $min, 'max' => $max])); } elseif ($min) { $input->getValidatorChain()->attach(new Validator\GreaterThan(['min' => $min])); } elseif ($max) { $input->getValidatorChain()->attach(new Validator\LessThan(['max' => $max])); } }
/** * Sets custom messages to filter input validators. * If provided $validators[], it will be filled with validator instances to be attached further on to a filter input, * otherwise the validator messages of the particular filter input provided will be amended accordingly. * * @param InputInterface $input * @param null|callback $inputLabel * @param null|array $validators */ public function setValidatorMessages(InputInterface $input, $inputLabel = null, &$validators = null) { if (!is_null($validators) && !is_array($validators)) { throw new \InvalidArgumentException(); } $inputLabel = is_callable($inputLabel) ? $inputLabel() : ''; foreach ($input->getValidatorChain()->getValidators() as $validator) { //set message to validator StringLength if (isset($validator['instance']) && $validator['instance'] instanceof Validator\StringLength) { $validatorInstance = is_null($validators) ? $validator['instance'] : clone $validator['instance']; $validatorInstance->setMessage(sprintf($this->translator->translate('The input %s is more than %%max%% characters long'), $this->translator->translate($inputLabel)), Validator\StringLength::TOO_LONG); if (is_array($validators)) { $validators[] = $validatorInstance; } } } }
/** * Add an input to the input filter * * @param array|Traversable|InputInterface|InputFilterInterface $input * @param null|string $name * @param $disablePurifier * @return InputFilter */ public function add($input, $name = null, $disablePurifier = false) { /** * By Default all Inputs will filter against XSS and invalid HTML, attributes and scripts */ $purifier = $this->getHtmlPurifier(); $input->getFilterChain()->attach(function ($value) use($purifier, $disablePurifier) { if ($purifier instanceof HTMLPurifier && !$disablePurifier) { if (is_array($value)) { foreach ($value as $key => $v) { $value[$key] = $purifier->purify($v); } return $value; } else { return $purifier->purify($value); } } return $value; }); return parent::add($input, $name); }
/** * @inheritdoc */ protected function attachDefaultValidators(InputInterface $input, DOMElement $element) { $input->getValidatorChain()->attach(new Uri()); }
/** * @param InputInterface $input * @return Input */ public function merge(InputInterface $input) { $this->setAllowEmpty($input->allowEmpty()); $this->setBreakOnFailure($input->breakOnFailure()); $this->setContinueIfEmpty($input->continueIfEmpty()); $this->setErrorMessage($input->getErrorMessage()); $this->setName($input->getName()); $this->setRequired($input->isRequired()); $this->setValue($input->getRawValue()); $filterChain = $input->getFilterChain(); $this->getFilterChain()->merge($filterChain); $validatorChain = $input->getValidatorChain(); $this->getValidatorChain()->merge($validatorChain); return $this; }
/** * @inheritdoc */ protected function attachDefaultValidators(InputInterface $input, DOMElement $element) { $input->getValidatorChain()->attach(new Validator\EmailAddress(['useMxCheck' => filter_var($element->getAttribute('data-validator-use-mx-check'), FILTER_VALIDATE_BOOLEAN)])); }
/** * Attach filters from data-filters attribute * * @param InputInterface $input * @param DOMElement $element */ protected function attachFilters(InputInterface $input, DOMElement $element) { $dataFilters = $element->getAttribute('data-filters'); if (!$dataFilters) { return; } $filters = $this->parseDataAttribute($dataFilters); foreach ($filters as $filter => $options) { // TODO: Needs to fixed when zend-inputfilter 3 is released. if (array_key_exists($filter, $this->filters)) { $class = $this->filters[$filter]; $input->getFilterChain()->attach(new $class($options)); } } }
/** * buildValidatorMessages * * @param $fieldName * @param InputInterface $input * * @return void */ protected function buildValidatorMessages($fieldName, InputInterface $input) { $validatorChain = $input->getValidatorChain(); $validators = $validatorChain->getValidators(); // We get the input messages because input does validations outside of the validators $allMessages = $input->getMessages(); foreach ($validators as $fkey => $validatorData) { /** @var \Zend\Validator\AbstractValidator $validator */ $validator = $validatorData['instance']; $params = []; if ($validator instanceof MessageParamInterface) { $params = $validator->getMessageParams(); } try { $messagesParams = $validator->getOption('messageParams'); $params = array_merge($params, $messagesParams); } catch (\Exception $exception) { // Do nothing } $inputMessages = $validator->getMessages(); // Remove the messages from $allMessages as we get them from the validators $allMessages = array_diff($allMessages, $inputMessages); $this->buildApiMessages($fieldName, $inputMessages, $params); } $params = []; if ($input instanceof MessageParamInterface) { $params = $input->getMessageParams(); } // get any remaining messages that did not come from validators $this->buildApiMessages($fieldName, $allMessages, $params); }