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,15 +48,32 @@ 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;
}
}
return True;
}
$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

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