Improve metadata type validation with error msgs #210
This commit is contained in:
parent
b40564263b
commit
bcbd8fb5c9
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -154,6 +154,10 @@ abstract class Metadata_Type {
|
|||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function add_error($msg) {
|
||||
$this->errors[] = $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue