Skip to content

Commit e8a448e

Browse files
committed
file: call validate on created object
This is the only safe place to call validate(); unfortunately this is after the creation of the object. Clean up by calling delete() on failure.
1 parent b923ecb commit e8a448e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/Skeleton/File/File.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ private static function create($action, $name, $content, $created = null) {
471471
$file->save();
472472

473473
$file_classname = get_called_class();
474-
return $file_classname::get_by_id($file->id);
474+
475+
// this is the first time the file will be available with it's resolved
476+
// classname, so we can only call validate reliably from now on.
477+
$file = $file_classname::get_by_id($file->id);
478+
479+
if (
480+
method_exists($file, 'validate') &&
481+
is_callable([$file, 'validate'])
482+
) {
483+
$errors = [];
484+
if ($file->validate($errors) === false) {
485+
$file->delete();
486+
throw new \Skeleton\Object\Exception_Validation($errors);
487+
}
488+
}
489+
490+
return $file;
475491
}
476492
}

0 commit comments

Comments
 (0)