Improve metadata type validation with error msgs #210

This commit is contained in:
Leo Germani 2019-03-17 14:41:05 -03:00
parent b40564263b
commit bcbd8fb5c9
2 changed files with 24 additions and 3 deletions

View File

@ -48,7 +48,13 @@ class Date extends Metadata_Type {
if (is_array($value)) {
foreach ($value as $date_value) {
$d = \DateTime::createFromFormat($format, $date_value);
if ( !($d && $d->format($format) === $date_value) ) {
if (!$d || $d->format($format) !== $value) {
$this->add_error(
sprintf(
__('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'),
$value
)
);
return false;
}
}
@ -56,7 +62,18 @@ class Date extends Metadata_Type {
}
$d = \DateTime::createFromFormat($format, $value);
return $d && $d->format($format) === $value;
if (!$d || $d->format($format) !== $value) {
$this->add_error(
sprintf(
__('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'),
$value
)
);
return false;
}
return true;
}
/**

View File

@ -154,6 +154,10 @@ abstract class Metadata_Type {
$this->description = $description;
}
public function add_error($msg) {
$this->errors[] = $msg;
}
/**
* @param $options
*/