/** * Validates the input value according to this attribute. * * @param InputInterface $input The input to validate * * @throws InvalidValueException If the value is not valid */ public static function validate(InputInterface $input) { $value = $input->val(); //Psr UploadeFileInterface validation if ($value instanceof UploadedFileInterface) { if ($value->getError() === UPLOAD_ERR_NO_FILE) { return; } return static::validateMime($value->getStream()->getMetadata('uri'), $input->attr('accept')); } if (empty($value['tmp_name'])) { return true; } static::validateMime($value['tmp_name'], $input->attr('accept')); }
/** * Validates the input value according to this attribute. * * @param InputInterface $input The input to validate * * @throws InvalidValueException If the value is not valid */ public static function validate(InputInterface $input) { $value = $input->val(); if ($input->attr('type') === 'file') { if ($value instanceof UploadedFileInterface) { $value = $value->getSize(); } else { $value = isset($value['size']) ? $value['size'] : null; } } $attr = $input->attr('required'); if (!empty($attr) && empty($value) && strlen($value) === 0) { throw new InvalidValueException(sprintf(static::$error_message, $attr)); } }
/** * Validates the input value according to this attribute. * * @param InputInterface $input The input to validate * * @throws InvalidValueException If the value is not valid */ public static function validate(InputInterface $input) { $value = $input->val(); if ($input->attr('type') === 'file') { if ($value instanceof UploadedFileInterface) { $value = $value->getClientFilename(); } else { $value = isset($value['name']) ? $value['name'] : null; } } $attr = str_replace('/', '\\/', $input->attr('pattern')); if (!empty($attr) && !empty($value) && !filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^{$attr}\$/")))) { throw new InvalidValueException(sprintf(static::$error_message, $attr)); } }
/** * Validates the datetime input value according to this attribute. * * @param InputInterface $input The input to validate * * @throws InvalidValueException If the value is not valid */ public static function validateDatetime(InputInterface $input) { $value = $input->val(); $attr = $input->attr('max'); if (!empty($attr) && strtotime($value) > strtotime($attr)) { throw new InvalidValueException(sprintf(static::$error_message, $attr)); } }
/** * Validates the input value according to this attribute. * * @param InputInterface $input The input to validate * * @throws InvalidValueException If the value is not valid */ public static function validate(InputInterface $input) { if (!($value = $input->val())) { return; } if ($input->attr('multiple')) { if (array_keys(array_diff_key(array_flip($value), $input->options()))) { throw new InvalidValueException(sprintf(static::$error_message)); } } elseif (!isset($input[$value])) { throw new InvalidValueException(sprintf(static::$error_message)); } }
/** * Validates the input value according to this attribute * * @param InputInterface $input The input to validate * * @return string|true True if its valid, string with the error if not */ public static function validate($input) { $value = $input->val(); if (empty($value['tmp_name'])) { return true; } $attr = $input->attr('accept'); $accept = array_map('trim', explode(',', $attr)); $filename = $value['tmp_name']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return array_search($mime, $accept) !== false ? true : sprintf(static::$error_message, $attr); }
/** * {@inheritdoc} */ public static function onAdd(InputInterface $input, $value) { switch ($input->attr('type')) { case 'datetime': case 'datetime-local': case 'date': case 'time': case 'month': case 'week': if (!date_create($value)) { throw new \InvalidArgumentException('This attribute must be a valid datetime'); } static::addDatetimeValidator($input); return $value; } if (!is_float($value) && !is_int($value)) { throw new \InvalidArgumentException('This attribute must be a float number'); } static::addValidator($input); return $value; }
/** * Validates the input value according to this attribute * * @param InputInterface $input The input to validate * * @return string|true True if its valid, string with the error if not */ public static function validate($input) { $value = $input->val(); $attr = $input->attr('max'); return empty($attr) || $value <= $attr ? true : sprintf(static::$error_message, $attr); }