Example #1
0
 /**
  * @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]));
     }
 }
 /**
  * Attach validators from data-validators attribute
  *
  * @param InputInterface $input
  * @param DOMElement     $element
  */
 protected function attachValidators(InputInterface $input, DOMElement $element)
 {
     $dataValidators = $element->getAttribute('data-validators');
     if (!$dataValidators) {
         return;
     }
     $validators = $this->parseDataAttribute($dataValidators);
     foreach ($validators as $validator => $options) {
         if (array_key_exists($validator, $this->validators)) {
             $class = $this->validators[$validator];
             $input->getValidatorChain()->attach(new $class($options));
         }
     }
 }
Example #3
0
 /**
  * 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;
             }
         }
     }
 }
Example #4
0
 /**
  * @inheritdoc
  */
 protected function attachDefaultValidators(InputInterface $input, DOMElement $element)
 {
     $input->getValidatorChain()->attach(new Uri());
 }
Example #5
0
 /**
  * @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;
 }
Example #6
0
 /**
  * @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)]));
 }
 /**
  * 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);
 }