mirror of
https://github.com/cosmocode/dokuwiki-plugin-struct.git
synced 2025-07-29 11:59:24 +00:00
Validation should happen on raw value
This just renames the parameter to make this more clear
This commit is contained in:
@ -458,12 +458,12 @@ abstract class AbstractBaseType {
|
||||
*
|
||||
* The function should return the value as it should be saved later on.
|
||||
*
|
||||
* @param string|int $value
|
||||
* @param string|int $rawvalue
|
||||
* @return int|string the cleaned value
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function validate($value) {
|
||||
return trim($value);
|
||||
public function validate($rawvalue) {
|
||||
return trim($rawvalue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,15 +56,15 @@ class Date extends AbstractBaseType {
|
||||
* This function needs to throw a validation exception when validation fails.
|
||||
* The exception message will be prefixed by the appropriate field on output
|
||||
*
|
||||
* @param string|int $value
|
||||
* @param string|int $rawvalue
|
||||
* @return int|string
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
list($value) = explode(' ', $value, 2); // strip off time if there is any
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
list($rawvalue) = explode(' ', $rawvalue, 2); // strip off time if there is any
|
||||
|
||||
list($year, $month, $day) = explode('-', $value, 3);
|
||||
list($year, $month, $day) = explode('-', $rawvalue, 3);
|
||||
if(!checkdate((int) $month, (int) $day, (int) $year)) {
|
||||
throw new ValidationException('invalid date format');
|
||||
}
|
||||
|
@ -49,13 +49,13 @@ class DateTime extends Date {
|
||||
* This function needs to throw a validation exception when validation fails.
|
||||
* The exception message will be prefixed by the appropriate field on output
|
||||
*
|
||||
* @param string|array $value
|
||||
* @param string|array $rawvalue
|
||||
* @return string
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = trim($value);
|
||||
list($date, $time) = explode(' ', $value, 2);
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = trim($rawvalue);
|
||||
list($date, $time) = explode(' ', $rawvalue, 2);
|
||||
$date = trim($date);
|
||||
$time = trim($time);
|
||||
|
||||
|
@ -59,27 +59,27 @@ class Decimal extends AbstractMultiBaseType {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $value
|
||||
* @param int|string $rawvalue
|
||||
* @return int|string
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
$value = str_replace(',', '.', $value); // we accept both
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
$rawvalue = str_replace(',', '.', $rawvalue); // we accept both
|
||||
|
||||
if((string) $value != (string) floatval($value)) {
|
||||
if((string) $rawvalue != (string) floatval($rawvalue)) {
|
||||
throw new ValidationException('Decimal needed');
|
||||
}
|
||||
|
||||
if($this->config['min'] !== '' && floatval($value) <= floatval($this->config['min'])) {
|
||||
if($this->config['min'] !== '' && floatval($rawvalue) <= floatval($this->config['min'])) {
|
||||
throw new ValidationException('Decimal min', floatval($this->config['min']));
|
||||
}
|
||||
|
||||
if($this->config['max'] !== '' && floatval($value) >= floatval($this->config['max'])) {
|
||||
if($this->config['max'] !== '' && floatval($rawvalue) >= floatval($this->config['max'])) {
|
||||
throw new ValidationException('Decimal max', floatval($this->config['max']));
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $rawvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
15
types/Lookup.php
Normal file
15
types/Lookup.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace dokuwiki\plugin\struct\types;
|
||||
|
||||
class Lookup extends Dropdown {
|
||||
|
||||
protected $config = array(
|
||||
'schema' => '',
|
||||
'field' => ''
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -27,18 +27,18 @@ class Mail extends Text {
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @param int|string $value
|
||||
* @param int|string $rawvalue
|
||||
* @return int|string
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
|
||||
$mail = $this->config['prefix'] . $value . $this->config['postfix'];
|
||||
$mail = $this->config['prefix'] . $rawvalue . $this->config['postfix'];
|
||||
if(!mail_isvalid($mail)) {
|
||||
throw new ValidationException('Mail invalid', $mail);
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $rawvalue;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,20 +16,20 @@ class Media extends AbstractBaseType {
|
||||
/**
|
||||
* Checks against the allowed mime types
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $rawvalue
|
||||
* @return int|string
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
|
||||
if(!trim($this->config['mime'])) return $value;
|
||||
if(!trim($this->config['mime'])) return $rawvalue;
|
||||
$allows = explode(',', $this->config['mime']);
|
||||
$allows = array_map('trim', $allows);
|
||||
$allows = array_filter($allows);
|
||||
|
||||
list(, $mime,) = mimetype($value, false);
|
||||
list(, $mime,) = mimetype($rawvalue, false);
|
||||
foreach($allows as $allow) {
|
||||
if(strpos($mime, $allow) === 0) return $value;
|
||||
if(strpos($mime, $allow) === 0) return $rawvalue;
|
||||
}
|
||||
|
||||
throw new ValidationException('Media mime type', $mime, $this->config['mime']);
|
||||
|
@ -47,11 +47,11 @@ class Page extends AbstractMultiBaseType {
|
||||
/**
|
||||
* Cleans the link
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $rawvalue
|
||||
* @return string
|
||||
*/
|
||||
public function validate($value) {
|
||||
return cleanID($value);
|
||||
public function validate($rawvalue) {
|
||||
return cleanID($rawvalue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,13 +15,13 @@ class Url extends Text {
|
||||
/**
|
||||
* The final string should be an URL
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $rawvalue
|
||||
* @return int|string|void
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
|
||||
$url = $this->buildURL($value);
|
||||
$url = $this->buildURL($rawvalue);
|
||||
|
||||
$schemes = getSchemes();
|
||||
$regex = '^(' . join('|', $schemes) . '):\/\/.+';
|
||||
@ -29,7 +29,7 @@ class Url extends Text {
|
||||
throw new ValidationException('Url invalid', $url);
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $rawvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,20 +16,20 @@ class User extends AbstractMultiBaseType {
|
||||
);
|
||||
|
||||
/**
|
||||
* @param string $value the user to validate
|
||||
* @param string $rawvalue the user to validate
|
||||
* @return int|string|void
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
|
||||
if($this->config['existingonly']) {
|
||||
/** @var \DokuWiki_Auth_Plugin $auth */
|
||||
global $auth;
|
||||
$info = $auth->getUserData($value, false);
|
||||
if($info === false) throw new ValidationException('User not found', $value);
|
||||
$info = $auth->getUserData($rawvalue, false);
|
||||
if($info === false) throw new ValidationException('User not found', $rawvalue);
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $rawvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,13 +18,13 @@ class Wiki extends AbstractBaseType {
|
||||
/**
|
||||
* Clean line endings
|
||||
*
|
||||
* @param int|string $value
|
||||
* @param int|string $rawvalue
|
||||
* @return int|string
|
||||
*/
|
||||
public function validate($value) {
|
||||
$value = parent::validate($value);
|
||||
$value = cleanText($value);
|
||||
return $value;
|
||||
public function validate($rawvalue) {
|
||||
$rawvalue = parent::validate($rawvalue);
|
||||
$rawvalue = cleanText($rawvalue);
|
||||
return $rawvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user