/** * Validates the input value according to this attribute * * @param Input $input The input to validate * * @return boolean|string True if its valid, string with the error if not */ public static function validate(Input $input) { $value = $input->val(); //File if ($input->attr('type') === 'file') { $value = isset($value['name']) && !empty($value['size']) ? $value['name'] : null; } $attr = $input->attr('required'); return empty($attr) || !empty($value) || strlen($value) > 0 ? true : sprintf(static::$error_message, $attr); }
/** * Callback used on add this attribute to an input * * @param Input $input The input in which the attribute will be added * @param mixed $value The value of this attribute * * @return boolean $value The value sanitized */ public static function onAdd(Input $input, $value) { if (!is_bool($value)) { throw new \InvalidArgumentException('The multiple value must be a boolean'); } if ($value && ($name = $input->attr('name')) && substr($name, -2) !== '[]') { $input->attr('name', $name . '[]'); } return $value; }
/** * Validates the input value according to this attribute * * @param Input $input The input to validate * * @return boolean|string True if its valid, string with the error if not */ public static function validate(Input $input) { $value = $input->val(); //File if ($input->attr('type') === 'file') { $value = isset($value['name']) ? $value['name'] : null; } $attr = str_replace('/', '\\/', $input->attr('pattern')); return empty($attr) || empty($value) || filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^{$attr}\$/"))) ? true : sprintf(static::$error_message, $attr); }
/** * Callback used on add this attribute to an input * * @param Input $input The input in which the attribute will be added * @param mixed $value The value of this attribute * * @return mixed $value The value sanitized */ public static function onAdd(Input $input, $value) { if ($input->attr('multiple') && substr($value, -2) !== '[]') { $value .= '[]'; } return $value; }
/** * Validates the input value according to this attribute * * @param Input $input The input to validate * * @return boolean|string True if its valid, string with the error if not */ public static function validate(Input $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); }
/** * Validates the datetime input value according to this attribute * * @param Input $input The input to validate * * @return boolean|string True if its valid, string with the error if not */ public static function validateDatetime(Input $input) { $value = $input->val(); $attr = $input->attr('min'); return empty($attr) || strtotime($value) >= strtotime($attr) ? true : sprintf(static::$error_message, $attr); }
/** * Validates the input value according to this attribute * * @param Input $input The input to validate * * @return boolean|string True if its valid, string with the error if not */ public static function validate(Input $input) { $value = $input->val(); $attr = $input->attr('maxlength'); return empty($attr) || strlen($value) <= $attr ? true : sprintf(static::$error_message, $attr); }